线程在android中退出错误

时间:2011-11-18 17:44:58

标签: android

请帮助解决此错误....在下面的代码中,获取信息功能正常工作,但它提示错误说线程在退出时捕获异常....我正在尝试使用选项卡主机和第一个标签页是以下...在此我显示一个进度对话框,直到我获取我的数据,然后在列表视图中显示它

public class History extends Activity implements OnItemClickListener
{
/** Called when the activity is first created. */
ListView list;

//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems;

//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
private String resDriver,resPassenger,ID;
private ProgressDialog dialog;
ArrayList<HashMap<String, Object>> listInfo = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> item;
JSONObject jDriver;
//JSONObject jPassenger;

// Make strings for logging
private final String TAG = this.getClass().getSimpleName();
private final String RESTORE = ", can restore state";
private final String state = "Home Screen taking care of all the tabs";
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Intent loginIntent = getIntent();
    ID = loginIntent.getStringExtra("ID");
    listItems = new ArrayList<String>();
    Log.i(TAG, "Started view active rides");
    setContentView(R.layout.searchresults);
    list = (ListView)findViewById(R.id.ListView01);
    list.setOnItemClickListener(this);
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems);

    list.setAdapter(adapter);
    getInfo();
}

函数getInfo用于启动一个显示对话框并启动http请求以获取某些数据的线程...

public void getInfo(){
    GetInfoThread checkUpdate = new GetInfoThread();
    checkUpdate.start();
    dialog = ProgressDialog.show(History.this, "Retrieving Info","Please Wait ...", true);
}

private class GetInfoThread extends Thread
{
    public void run() {
        jDriver = new JSONObject();
        try {
            jDriver.put("ID", ID);
            jDriver.put("task", "GET DATES");

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        listItems = new ArrayList<String>();
        Log.i(TAG,"Sending data for the driver rides");
        resDriver = HTTPPoster.sendJson(jDriver,"http://dsadsada"); // Any Server URL

        JSONObject driver;
        try {
            driver = new JSONObject(resDriver);
            Log.i(TAG,"Recieved Driver details");

                    listItems.add(array[0]);
                    handler.sendEmptyMessage(0);
                }
            }
        } catch (JSONException e) {
        // TODO Auto-generated catch block
            listItems.add("No driver rides created");
            handler.sendEmptyMessage(0);
        }

    }

    private Handler handler = new Handler() {
        @Override
       public void handleMessage(Message msg) {
           dialog.dismiss();
           Log.i(TAG,"hello 123");
           adapter.notifyDataSetChanged();
       }
    };
}

1 个答案:

答案 0 :(得分:1)

我不确定究竟是什么导致了您的错误,但我怀疑它与未在实际UI线程上运行的UI更改有关。在Android中有一个名为AsyncTask的类,它将为您执行线程处理并处理后台线程和UI线程之间的数据传递。我建议您重写代码以使用AsyncTask类。