就是在运行过程中,点击发送按钮的时候就闪退了。
报错情况大概是这样:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.example.tulingdemo, PID: 5775
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$4.done(AsyncTask.java:415)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)...
代码如下:
public class MainActivity extends AppCompatActivity implements HttpGetDataListener, View.OnClickListener {
private HttpDate httpDate;
private List<ListData> lists;
private ListView lv;
private EditText sendtext;
private Button send_btn;
private String content_str;
private TextAdapter adapter;
private String[] welcome_array;
private double currentTime,oldtime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
lv = (ListView) findViewById(R.id.lv);
sendtext = (EditText) findViewById(R.id.sendText);
send_btn = (Button) findViewById(R.id.send_btn);
lists = new ArrayList<ListData>();
send_btn.setOnClickListener((View.OnClickListener) this);
adapter = new TextAdapter(lists,this);
lv.setAdapter(adapter);
ListData listData;
listData = new ListData(getRandomWelcomeTips(),ListData.RESEIVE,getTime());
lists.add(listData);
}
private String getRandomWelcomeTips(){
String welcome_tip = null;
welcome_array = this.getResources().getStringArray(R.array.welcome_tips);
int index = (int) (Math.random()*(welcome_array.length -1));
welcome_tip = welcome_array[index];
return welcome_tip;
}
@Override
public void getDataUrl(String data){
//System.out.println(data);
parseText(data);
}
public void parseText(String data){
try {
JSONObject jb = new JSONObject();
ListData listData;
listData = new ListData(jb.getString("text"),ListData.RESEIVE,getTime());
lists.add(listData);
adapter.notifyDataSetChanged();
}catch (JSONException e){
e.printStackTrace();
}
}
public void onClick(View v){
getTime();
content_str = sendtext.getText().toString();
sendtext.setText("");
String dropk = content_str.replace(" ","");
String droph = dropk.replace("\n","");
ListData listData;
listData = new ListData(content_str,ListData.SEND,getTime());
lists.add(listData);
if(lists.size()>30){
for(int i=0;i<lists.size();i++){
lists.remove(i);
}
}
adapter.notifyDataSetChanged();
httpDate = (HttpDate) new HttpDate("http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好"+ content_str,this).execute();
}
private String getTime(){
currentTime = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date();
String str = format.format(curDate);
if(currentTime - oldtime >= 5*60*1000){
oldtime = currentTime;
return str;
}else {
return "";
}
}
}
帮忙看看问题在哪吧!
集合是不能用for循环来遍历删除的,可以用迭代器。而且你既然每个下标都删除,为什么不用.clear()?要一个一个删?