0

我在 mt AsyncTask doInBackground 方法中有循环。

  @Override
        protected Void doInBackground(Void... params) {

            receive = new Vector<Sms>();
            sent = new Vector<Sms>();
            draft = new Vector<Sms>();

            try {

                String type, from, date, budy;
                Vector<Sms> receive = new Vector<Sms>();
                Vector<Sms> sent = new Vector<Sms>();
                Vector<Sms> draft = new Vector<Sms>();

                progress_status = 1;
                publishProgress(progress_status);

                Cursor cursor = mContext.getContentResolver().query(
                        Uri.parse("content://sms"), null, null, null, null);;
                cursor.moveToFirst();
                progress_status = 2;
                publishProgress(progress_status);


                do {
                    type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
                    from = cursor
                            .getString(cursor.getColumnIndexOrThrow("address"));
                    date = cursor.getString(cursor.getColumnIndexOrThrow("date"));
                    budy = cursor.getString(cursor.getColumnIndexOrThrow("body"));

                    // check type (1-inbox,2-outbox,3-draft) and set there views
                    int numType = Integer.parseInt(type);

                    switch (numType) {
                    case 1:
                        Log.d("inbox", "message");  
                        Log.d("inbox", numType + "");
                        //receive.add(new Sms(type, from, date, budy)); 
                        break;
                    case 2:
                        Log.d("outbox", "message");
                        Log.d("outbox", numType + "");
                        sent.add(new Sms(type, from, date, budy));
                        break;
                    case 3:
                        Log.d("draft", "message");
                        Log.d("draft", numType + "");
                        //draft.add(new Sms(type, from, date, budy));
                        break;

                    default:
                        break;
                    }

                } while (cursor.moveToNext());

            } catch (Exception e) {
                Log.d("eeeee", e.getMessage());
            }







            return null;
        }

此开关在循环内,如果我删除矢量动作(在接收和发送时)它将运行完美。

但我确实想更新向量,但是当我这样做时我捕获了异常。

“无法在未调用 Looper.prepare() 的线程内创建处理程序”

因此,在我发布的这段代码中,我将接收放在注释中,并且发送的向量处于活动状态,因此循环将运行,直到第一个发送到达循环,因此日志猫将如下所示:

04-13 00:40:41.000: D/inbox(13953): message
04-13 00:40:41.000: D/inbox(13953): 1
04-13 00:40:41.000: D/inbox(13953): message
04-13 00:40:41.000: D/inbox(13953): 1
04-13 00:40:41.005: D/outbox(13953): message
04-13 00:40:41.005: D/outbox(13953): 2
04-13 00:40:41.005: D/eeeee(13953): Can't create handler inside thread that has not called Looper.prepare()

正如我所说,单独的循环不会只对vector.add造成麻烦...

这是 e.printStackTrace();

04-13 01:01:08.080: W/System.err(14901): java.lang.RuntimeException: Can't create       handler inside thread that has not called Looper.prepare()
04-13 01:01:08.085: W/System.err(14901):    at android.os.Handler.<init>(Handler.java:121)
04-13 01:01:08.085: W/System.err(14901):    at android.app.Activity.<init>(Activity.java:739)
 04-13 01:01:08.085: W/System.err(14901):   at com.example.sms.Sms.<init>(Sms.java:18)
04-13 01:01:08.085: W/System.err(14901):    at com.example.sms.MainActivity$ShowDialogAsyncTask.doInBackground(MainActivity.java:168)
04-13 01:01:08.085: W/System.err(14901):    at com.example.sms.MainActivity$ShowDialogAsyncTask.doInBackground(MainActivity.java:1)
04-13 01:01:08.085: W/System.err(14901):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-13 01:01:08.085: W/System.err(14901):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-13 01:01:08.085: W/System.err(14901):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-13 01:01:08.085: W/System.err(14901):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-13 01:01:08.085: W/System.err(14901):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-13 01:01:08.085: W/System.err(14901):    at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

0

如果您不将 Activity 用作“屏幕”,则不应扩展它。您已经删除SMS extends Activity并以更以数据为中心的方式设计 SMS。活动用于显示聚合数据而不是存储它。

让短信是这样的:

public class SMS {

    public String type, from, mdate, body;

    public SMS(String type, String from, String mdate, String body) {
        this.type = type;
        this.from = from;
        this.mdate = mdate;
        this.body = body;
    }
}
于 2013-04-12T22:17:02.513 回答