android-从Activity.onCreate()创建线程永不死

我花了最后几个小时在这里寻找答案,但似乎没有什么对我来说清楚.

这是我的问题:我有一个带有主菜单的简单应用.其中一个选项可从PHP服务器检索注释列表,更新数据库,然后显示ListView.

内部一切正常.现在,每次我按回来然后再次启动活动时,都会启动一个新线程.我设法达到了50个以上的等待线程.我正在使用Eclipse的DDMS标签来监视胎面.

如果我尝试调用Looper.getLooper().quit()或Looper.myLooper().quit(),则会收到错误消息,提示“不允许退出主线程”.

我在做什么错,应该在哪里/如何停止线程?

这是代码

public class RequestActivity extends Activity {

    ProgressDialog pDialog;
    DatabaseHelper db;
    int userId;
    myCursorAdapter adapter;
    Thread runner = null;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userId = myApplication.getInstance().getSession().getUserId();
        setContentView(R.layout.request_list);

        db = new myProvider.DatabaseHelper(this);
        Cursor cursor = db.getRequests(userId);
        startManagingCursor(cursor);
        adapter = new myCursorAdapter(RequestActivity.this, cursor);
        list = (ListView) findViewById(R.id.list);
        list.setVisibility(View.INVISIBLE);
        list.setAdapter(adapter);

        pDialog = ProgressDialog.show(RequestActivity.this, "", "Loading ...", true, false);

        runner = new Thread() {
            Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    adapter.getCursor().requery(); 
                }
            };  

            public void run() {
                Looper.prepare();
                // ... setup HttpGet
                try {
                    // ... get HTTP GET response 
                    if (response != null) {
                        // ... update database
                        handler.sendEmptyMessage(0);
                    }
                } catch(Exception e) {
                    // .. log exception
                }
                Looper.loop();
            }
        };
        runner.start();
    }

    private static class ViewHolder {
        // .. view objects
    }

    private class myCursorAdapter extends CursorAdapter {
        private LayoutInflater inflater;
        public myCursorAdapter(Context context, Cursor c) {
            super(context, c);
            inflater = LayoutInflater.from(context);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // .. bind data
            if (cursor.isLast()) {
                pDialog.dismiss();
                list.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = inflater.inflate(R.layout.request_list_item, parent, false);
            ViewHolder holder = new ViewHolder();
            // .. set holder View objects
            view.setTag(holder);
            return view;
        }
    }

}

解决方法:

在onDestroy方法上,您是否进行了任何调用来停止线程?

public void onDestroy()
{
    Thread moribund = runner;
    runner = null
    moribund.interrupt();

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...