-5

我想显示所有短信..所以我运行 loadIntialMessage() 来显示 20 条短信..

在这种方法上,我将运行线程背景以显示下一条短信。这是在 loadIntialmessage() 方法的列表滚动方法上运行的 AsyncTask 类。

    public void loadInitialMessages() {
    String[] columns = new String[] { "_id", "thread_id", "date", "body",
            "address", "type", "read" };
    String orderby;
    Log.d(TAG, "sory index" + sort_index);
    // sort_index =1;
    if (sort_index == 0) {
        orderby = "date DESC";
    } else {
        orderby = "date ASC";
    }
    for (int i = 0; i < ((IdSMS.size() > NO_OF_SMS_TO_LOAD) ? NO_OF_SMS_TO_LOAD
            : IdSMS.size()); i++) {

        Cursor c = getContentResolver().query(Uri.parse(SMS_URI_ALL),
                columns, " _id =" + IdSMS.get(i), null, null);
        while (c.moveToNext()) {
            String contactname = getContactDisplayNameByNumber((c
                    .getString(c.getColumnIndexOrThrow("address"))));
            // Log.d(TAG, "contact number without replace space" +
            // contactname);
            if (contactname.contains(" ")) {
                contactname.replace(" ", "");
                // Log.d(TAG, "contact number  replace space" +
                // contactname);
            }
            if (!NumberListContact.contains(contactname)) {

                NumberListContact.add(contactname);
                IdSMS.add(c.getString(c.getColumnIndexOrThrow("_id"))
                        .toString());
                ThreadSMS.add(c.getString(
                        c.getColumnIndexOrThrow("thread_id")).toString());
                SMSDate.add(c.getString(c.getColumnIndexOrThrow("date"))
                        .toString());
                MessageList.add(c
                        .getString(c.getColumnIndexOrThrow("body"))
                        .toString());
                NumberList.add(c.getString(
                        c.getColumnIndexOrThrow("address")).toString());
                SMSType.add(c.getString(c.getColumnIndexOrThrow("type"))
                        .toString());
                SMSRead.add(c.getString(c.getColumnIndexOrThrow("read"))
                        .toString());
                Log.d(TAG,
                        "read and unread"
                                + c.getString(
                                        c.getColumnIndexOrThrow("read"))
                                        .toString());
            }
            Log.d(TAG, "Counter Value " + counter);
            Log.d(TAG,
                    " thread_Id "
                            + c.getString(
                                    c.getColumnIndexOrThrow("thread_id"))
                                    .toString());

            counter++;
        }
        c.close();
    }

    // refreshAdapter();
    inboxAdaptor = new inboxAdaptor(getApplicationContext(), IdSMS,
            ThreadSMS, NumberListContact, NumberList, MessageList, SMSDate,
            SMSType, SMSRead);

    lvmsg.setAdapter(inboxAdaptor);

    inboxAdaptor.notifyDataSetChanged();

    lvmsg.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub


        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub

            if (MessageList.size()
                    - (int) (firstVisibleItem + visibleItemCount) <= 1) {
                Log.d("onScroll", " firstVisibleItem " + firstVisibleItem);
                Log.d("onScroll", " visibleItemCount " + visibleItemCount);
                Log.d("onScroll", " totalItemCount " + totalItemCount);

                Log.d(TAG,
                        "Adding more data to listview ---------------------->>");

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        LoadNewMessage myTask = new LoadNewMessage(
                                MainActivity.this);
                        myTask.execute();
                    }
                });
                // if(!isLoadingNewMessages){
                // mLoadNewMessage.post(new Runnable() {
                //
                // @Override
                // public void run() {
                // // TODO Auto-generated method stub
                // isLoadingNewMessages=true;
                // addToListview();
                //
                // }
                // });
                // }

            }
        }
    });

}

class LoadNewMessage extends AsyncTask<Void, Void, Void> {

    ProgressDialog pd;
    Context context;

    public LoadNewMessage(Context context) {
        this.context = context;
        pd = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {
            addToListview();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void... params) {

        inboxAdaptor.notifyDataSetChanged();


    }

}

`

在 addToListview() 我显示下一条短信..

public void addToListview() {
    String[] columns = new String[] { "_id", "thread_id", "date", "body",
            "address", "type", "read" };
    String orderby = "date DESC limit 10";
    if (sort_index == 1) {
        orderby = "date ASC";
    } else {
        orderby = "date DESC";
    }
    // Log.d(TAG, " Adding new values Counter "+counter);

    for (int i = 0; i < (((IdSMS.size() - counter) > NO_OF_SMS_TO_LOAD) ? NO_OF_SMS_TO_LOAD
            : (IdSMS.size() - counter)); i++) {

        Cursor c = getContentResolver().query(Uri.parse(SMS_URI_ALL),
                columns, " _id =" + IdSMS.get(counter), null, null);
        // Log.d(TAG, " Adding new values cursor count "+c.getCount());
        while (c.moveToNext()) {
            String contactname = getContactDisplayNameByNumber((c
                    .getString(c.getColumnIndexOrThrow("address"))));

            if (!NumberListContact.contains(contactname)) {

                NumberListContact.add(contactname);
                IdSMS.add(c.getString(c.getColumnIndexOrThrow("_id"))
                        .toString());
                ThreadSMS.add(c.getString(
                        c.getColumnIndexOrThrow("thread_id")).toString());
                SMSDate.add(c.getString(c.getColumnIndexOrThrow("date"))
                        .toString());
                MessageList.add(c
                        .getString(c.getColumnIndexOrThrow("body"))
                        .toString());
                NumberList.add(c.getString(
                        c.getColumnIndexOrThrow("address")).toString());
                SMSType.add(c.getString(c.getColumnIndexOrThrow("type"))
                        .toString());
                SMSRead.add(c.getString(c.getColumnIndexOrThrow("read"))
                        .toString());
            }

        }
        c.close();
        counter++;
    }

    isLoadingNewMessages = false;
    // refreshAdapter();
}

但是当我在消息加载时单击任何其他按钮时出现错误..

   03-15 13:01:35.334: W/System.err(283): java.lang.NullPointerException
   03-15 13:01:35.334: W/System.err(283):   at com.example.smsmanager.MainActivity.addToListview(MainActivity.java:325)
   03-15 13:01:35.344: W/System.err(283):   at com.example.smsmanager.MainActivity$LoadNewMessage.doInBackground(MainActivity.java:139)
   03-15 13:01:35.344: W/System.err(283):   at com.example.smsmanager.MainActivity$LoadNewMessage.doInBackground(MainActivity.java:1)
   03-15 13:01:35.344: W/System.err(283):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
   03-15 13:01:35.344: W/System.err(283):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
   03-15 13:01:35.344: W/System.err(283):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
   03-15 13:01:35.344: W/System.err(283):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
   03-15 13:01:35.344: W/System.err(283):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
   03-15 13:01:35.344: W/System.err(283):   at java.lang.Thread.run(Thread.java:1096)
4

2 回答 2

0

这是我的猜测。除非您发布代码,否则不能非常具体:

在 doInBackground 方法中,它调用 MainActivity.addToListview 方法,该方法修改了 UI 并且某些对象尚未准备好。空对象可能取决于您的异步任务完成还是您忘记设置它?

于 2013-03-21T07:18:52.510 回答
0

这是我的猜测:您正在尝试从不在 UI 线程上的 AsyncTask 更新 UI。您要做的就是向 Handler 发送消息并让 Handler 更新 UI。

将此添加到您的类变量中:

private Handler handler = new Handler();

Message注意:您可以通过对象或类变量发送各种数据

将此添加到您的 doInBackground() 函数中:

handler.sendEmptyMessage(0);

使用添加到您的类的函数更新您的 UI,如下所示:

private final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        // Do something to the UI here.
    }
};
于 2013-03-21T07:25:35.567 回答