1 package com.briup.client;
2 import java.util.ArrayList;
3 import java.util.Calendar;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.apache.http.HttpEntity;
8 import org.apache.http.HttpResponse;
9 import org.apache.http.message.BasicNameValuePair;
10
11 import android.app.Activity;
12 import android.app.DatePickerDialog;
13 import android.app.ProgressDialog;
14 import android.app.DatePickerDialog.OnDateSetListener;
15 import android.os.Bundle;
16 import android.os.Looper;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.widget.ArrayAdapter;
20 import android.widget.Button;
21 import android.widget.DatePicker;
22 import android.widget.EditText;
23 import android.widget.Spinner;
24 import android.widget.TextView;
25 import android.widget.Toast;
26
27 import com.briup.pojo.RecordIn;
28 import com.briup.utils.HttpUtil;
29 import com.briup.utils.ProgressDialogUtil;
30 import com.briup.utils.XmlUtil;
31
32
33 public class ModifyInActivity extends Activity {
34 private String intype_url;
35 private Spinner spinner_inType;
36 private String username;
37 private TextView tvDate;
38 private Button btnDate;
39 private int m_year,m_month,m_day;
40 private Calendar c;
41 private Button btnSubmit,btnReturn;
42 private EditText et_inMount,et_Detail;
43 private ProgressDialogUtil pdu;
44 private String modifyin_url;
45 private List<Map<String,String>> list=null;
46 private RecordIn recordin;
47 private String in_type;
48 @Override
49 protected void onCreate(Bundle savedInstanceState) {
50 // TODO Auto-generated method stub
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.modify_in);
53 intype_url=getResources().getString(R.string.basic_url)+"intype";
54 modifyin_url=getResources().getString(R.string.basic_url)+"modifyin";
55 recordin=(RecordIn)getIntent().getExtras().getSerializable("recordin");
56 username=getIntent().getExtras().getString("username");
57 c=Calendar.getInstance();
58 m_year=c.get(Calendar.YEAR);
59 m_month=c.get(Calendar.MONTH);
60 m_day=c.get(Calendar.DAY_OF_MONTH);
61 spinner_inType=(Spinner)findViewById(R.id.spinner_editInType);
62 tvDate=(TextView)findViewById(R.id.tv_editDateIn);
63 btnDate=(Button)findViewById(R.id.editBtnDateIn);
64 btnSubmit=(Button)findViewById(R.id.editBtnSubmit);
65 btnReturn=(Button)findViewById(R.id.editBtnReturn);
66 et_inMount=(EditText)findViewById(R.id.et_editInMount);
67 et_Detail=(EditText)findViewById(R.id.et_editDetail);
68 btnDate.setOnClickListener(new OnClickListener(){
69 @Override
70 public void onClick(View v) {
71 // TODO Auto-generated method stub
72 new DatePickerDialog(ModifyInActivity.this,new OnDateSetListener(){
73 @Override
74 public void onDateSet(DatePicker view, int year, int monthOfYear,
75 int dayOfMonth) {
76 // TODO Auto-generated method stub
77 m_year=year;
78 m_month=monthOfYear;
79 m_day=dayOfMonth;
80 tvDate.setText(m_year+":"+(m_month+1)+":"+m_day);
81 }
82
83 },m_year,m_month,m_day).show();
84 }});
85 btnSubmit.setOnClickListener(new OnClickListener(){
86 @Override
87 public void onClick(View v) {
88 // TODO Auto-generated method stub
89 //System.out.println("in_type:"+in_type);
90 pdu=new ProgressDialogUtil(ModifyInActivity.this);
91 pdu.show(ProgressDialog.STYLE_SPINNER,null,"正在保存...",R.drawable.dialog,true);
92 new Thread(){
93 public void run(){
94 Looper.prepare();
95 String in_typeStr=spinner_inType.getSelectedItem().toString();
96 boolean flag=true;
97 for(int i=0;i<list.size()&&flag;i++){
98 Map<String,String> map=list.get(i);
99 for(String key:map.keySet()){
100 if(map.get(key).equals(in_typeStr)){
101 flag=false;
102 in_type=key;
103 break;
104 }
105 }
106 }
107 String in_mount=et_inMount.getText().toString();
108 String date=tvDate.getText().toString();
109 String detail=et_Detail.getText().toString().trim().length()==0?"无":et_Detail.getText().toString().trim();
110 List<BasicNameValuePair> parms=new ArrayList<BasicNameValuePair>();
111 parms.add(new BasicNameValuePair("id",recordin.getId()));
112 parms.add(new BasicNameValuePair("in_type",in_type));
113 parms.add(new BasicNameValuePair("in_mount",in_mount));
114 parms.add(new BasicNameValuePair("date",date));
115 parms.add(new BasicNameValuePair("detail",detail));
116 HttpResponse response=HttpUtil.post(modifyin_url, parms);
117 if(response!=null&&response.getStatusLine().getStatusCode()==200){
118 //从response中获得响应信息
119 HttpEntity entity=response.getEntity();
120 Map<String,String> map=null;
121 try {
122 map=XmlUtil.parseXml(entity.getContent());
123 } catch (Exception e) {
124 e.printStackTrace();
125 }
126 if(map.get("message")!=null){//保存成功
127 Toast.makeText(ModifyInActivity.this, "修改成功", Toast.LENGTH_LONG).show();
128 ModifyInActivity.this.finish();
129 }
130 if(map.get("error")!=null){
131 Toast.makeText(ModifyInActivity.this, "修改失败,请重试", Toast.LENGTH_LONG).show();
132 }
133 pdu.cancel();
134 }
135 else{
136 Toast.makeText(ModifyInActivity.this, "网络异常", Toast.LENGTH_LONG).show();
137 pdu.cancel();
138 }
139 Looper.loop();
140 }
141 }.start();
142 }});
143 btnReturn.setOnClickListener(new OnClickListener(){
144 @Override
145 public void onClick(View v) {
146 // TODO Auto-generated method stub
147 ModifyInActivity.this.finish();
148 }});
149 et_inMount.setText(recordin.getIn_mount());
150 tvDate.setText(recordin.getRecordtime());
151 et_Detail.setText(recordin.getDetail());
152 new Thread(){
153 public void run(){
154 Looper.prepare();
155 List<BasicNameValuePair> parms=new ArrayList<BasicNameValuePair>();
156 parms.add(new BasicNameValuePair("username",username));
157 HttpResponse response=HttpUtil.post(intype_url, parms);
158 if(response!=null&&response.getStatusLine().getStatusCode()==200){
159 //从response中获得响应信息
160 HttpEntity entity=response.getEntity();
161 try {
162 list=XmlUtil.loadInType(entity.getContent());
163 if(list!=null&&list.size()>0){
164 String[] array=new String[list.size()];
165 for(int i=0;i<list.size();i++){
166 Map<String,String> map=list.get(i);
167 for(String key:map.keySet()){
168 array[i]=map.get(key);
169 }
170 }
171 ArrayAdapter<String> adapter=new ArrayAdapter<String>(ModifyInActivity.this,
172 android.R.layout.simple_spinner_item,
173 array);
174 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
175 spinner_inType.setAdapter(adapter);
176 int position=0;
177 for(int i=0;i<array.length;i++){
178 if(array[i].equals(recordin.getIn_name())){
179 position=i;
180 break;
181 }
182 }
183 System.out.println("position in Spinner_InType-------->"+position);
184 spinner_inType.setSelection(position);
185 }
186 } catch (Exception e) {
187 e.printStackTrace();
188 }
189 }
190 Looper.loop();
191 }
192 }.start();
193 }
194
195 }
红色的部分是出现的错误,为运行时异常,但就是不知道问题出现在哪里,并且从上一个Activity获取到的数据和从服务器获得的
数据都没有错,不知道是不是因为使用了线程的原因,求路过的高人指点一下,感激不尽,这个问题小弟己经纠结了好久了,调试的时候
问题时有时无。。。。。。。。。。
建议在代码中插入Log.v()来记录程序运行过程,看是哪个地方出错的
调试了好久,实在不知道是哪里的问题,根据报告的错误信息,可能是设置ArrayAdapter的问题,但看代码,实在看不出有什么不正确的地方,求解
我终于知道问题出在哪里,由于我在主线程中启动了一个子线程去更新主线程中的Spinner控件,所以会报错,我用Handler进行处理后,就没有运行时异常了