我花了最后几个小时在这里寻找答案,但似乎没有什么对我来说清楚.
这是我的问题:我有一个带有主菜单的简单应用.其中一个选项可从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-从Activity.onCreate()创建线程永不死全部内容,希望文章能够帮你解决android-从Activity.onCreate()创建线程永不死所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)