线程的Andr​​oid错误退出 [英] thread exiting error in android

查看:109
本文介绍了线程的Andr​​oid错误退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请与此错误帮助....在以下code中的获取信息功能的正常工作,但它给出了一个错误说的线程退出捕获的异常....我想用一个标签主机和第一个选项卡页面如下...在这我展示一个进度对话框,直到我得到我的数据,然后将其显示在列表视图

 公共类历史扩展活动实现OnItemClickListener
{
/ **当第一次创建活动调用。 * /
ListView控件列表;// LIST字符串数组将作为列表项
ArrayList的<串GT; listItems中;//定义STRING适配器,它可以处理的ListView数据
ArrayAdapter<串GT;适配器;
私人字符串resDriver,resPassenger,身份证;
私人ProgressDialog对话框;
ArrayList的<&HashMap的LT;弦乐,对象>>列表信息=新的ArrayList<&HashMap的LT;弦乐,对象>>();
HashMap的<弦乐,对象>项目;
的JSONObject jDriver;
//的JSONObject jPassenger;//使测井串
私人最终字符串标记= this.getClass()getSimpleName()。
私人最终字符串RESTORE =,可以恢复状态;
私人最终字符串状态=主屏幕把所有的标签关怀;
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    意向loginIntent = getIntent();
    ID = loginIntent.getStringExtra(ID);
    时listItems =新的ArrayList<串GT;();
    Log.i(TAG,开始积极看法乘坐);
    的setContentView(R.layout.searchresults);
    名单=(ListView控件)findViewById(R.id.ListView01);
    list.setOnItemClickListener(本);
    适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,listItems中);    list.setAdapter(适配器);
    获取信息();
}

功能的getInfo 用来启动一个线程,显示一个对话框,并启动一个HTTP请求到得到一些数据...

 公共无效的getInfo(){
    GetInfoThread checkUpdate =新GetInfoThread();
    checkUpdate.start();
    对话框= ProgressDialog.show(History.this,检索信息,请稍候...,真正的);
}私有类GetInfoThread继承Thread
{
    公共无效的run(){
        jDriver =新的JSONObject();
        尝试{
            jDriver.put(ID,ID);
            jDriver.put(任务,GET日期表);        }赶上(JSONException E1){
            // TODO自动生成catch块
            e1.printStackTrace();
        }
        时listItems =新的ArrayList<串GT;();
        Log.i(TAG,发送数据为驱动的游乐设施);
        resDriver = HTTPPoster.sendJson(jDriver,的http:// dsadsada); //任何服务器URL        司机的JSONObject;
        尝试{
            司机=新的JSONObject(resDriver);
            Log.i(TAG,收到驱动程序详细信息);                    listItems.add(数组[0]);
                    handler.sendEmptyMessage(0);
                }
            }
        }赶上(JSONException E){
        // TODO自动生成catch块
            listItems.add(创建无驱动的游乐设施);
            handler.sendEmptyMessage(0);
        }    }    私人处理程序处理程序=新的处理程序(){
        @覆盖
       公共无效的handleMessage(消息MSG){
           dialog.dismiss();
           Log.i(TAG,你好123);
           adapter.notifyDataSetChanged();
       }
    };
}


解决方案

我不知道究竟是什么原因造成您的错误,但我怀疑它与用户界面的变化而不是实际的UI线程上运行的事情。在Android中有一个叫 AsyncTask的类,将做穿线为你和处理数据的后台线程的UI线程之间的传递。我建议你​​重写code到利用 AsyncTask的类。

Please help with this error .... In the following code the get info function works correctly but it gives an error saying the thread caught an exception at exiting.... I am trying to use a tab host and the first tab page is the following... In this i show a progress dialog until i get my data and then show it in a list view

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();
}

The function getInfo is used to start a thread which shows a dialog box and starts a http request to get some data ...

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();
       }
    };
}

解决方案

I am not sure exactly what is causing your error but I suspect it has to do with UI changes not running on the actual UI thread. In Android there is a class called AsyncTask that will do the threading for you and handle the passing of data between the background thread an the UI thread. I would suggest rewriting your code to utilize the AsyncTask class.

这篇关于线程的Andr​​oid错误退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆