0

运行 doinbackground 方法时出现空指针异常。这是我的错误![在此处输入图像描述][1]

这是我的错误:::

FAIAL EXCEPTION: AsyncIask #1

java.1ang.RuntimeException: An error occured while executing doInBa 45
ckground()

at android.os.AsyncIask$3.done(AsyncIask.java:278)

at java.uti1.concurrent.FutureIask$Sync.innerSetException(FutureIa 45
sk.java:273)

at java.uti1.concurrent.FutureIask.setException(FutureIask.java:12 45
4)

at java.uti1.concurrent.FutureIask$Sync.innerRun(FutureIask.java:3 45
07)

at java.uti1.concurrent.FutureTask.run(FutureIask.java:137)

at android.os.AsyncIask$Seria1Executor$1.run(AsyncIask.java:208)

at java.uti1.concurrent.ThreadPoo1Executor.runworker(IhreadPoo1Exe 45
cutor.java:1076)

at java.uti1.concurrent.IhreadPoo1Executor$Worker.run(IhreadPoo1Ex 45
ecutor.java:569)

at java.1ang.Ihread.run(Ihread.java:856)

Caused by: java.lang.NullPointerException

at com.examp1e.outgoingsms.OutgoingSmsListener$OutSmsLogger.doInBa 45
ckground(OutgoingSmsListener.java:45)

at com.examp1e.outgoingsms.OutgoingSmsListener$OutSmsLogger.doInBa 45
ckground(OutgoingSmsListener.java:1)

at android.os.AsyncIask$2.call(AsyncIask.java:264)

at java.uti1.concurrent.FutureIask$Sync.innerRun(FutureIask.java:3 45
05)

5 more

这是我的代码..

public class OutgoingSmsListener extends BroadcastReceiver {

public Context context;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    new OutSmsLogger(context).execute();
}

public class OutSmsLogger extends AsyncTask<Void, Void, Void> {
    private final Uri SMS_URI = Uri.parse("content://sms");
    private final String[] COLUMNS = new String[] {"date", "address", "body", "type"};
    private static final String CONDITIONS = "type = 2 AND date > ";
    private static final String ORDER = "date DESC";

    private SharedPreferences prefs;
    private long timeLastChecked;
    private Cursor cursor;

    public OutSmsLogger(Context context) {
        this.prefs = context.getSharedPreferences("some_file_name", Context.MODE_PRIVATE);
    }


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        timeLastChecked = prefs.getLong("time_last_checked", -1L);
        ContentResolver cr = context.getContentResolver();

        // get all sent SMS records from the date last checked, in descending order
        cursor = cr.query(SMS_URI, COLUMNS, CONDITIONS + timeLastChecked, null, ORDER);

        // if there are any new sent messages after the last time we checked
        if (cursor.moveToNext()) {
            Set<String> sentSms = new HashSet<String>();
            timeLastChecked = cursor.getLong(cursor.getColumnIndex("date"));
            do {
                long date = cursor.getLong(cursor.getColumnIndex("date"));
                String address = cursor.getString(cursor.getColumnIndex("address"));
                String body = cursor.getString(cursor.getColumnIndex("body"));
                String thisSms = date + "," + address + "," + body;

                if (sentSms.contains(thisSms)) {
                    continue; // skip that thing
                }

                // else, add it to the set
                sentSms.add(thisSms);
                Log.d("Test", "date sent: " + date);
                Log.d("Test", "target number: " + address);
                Log.d("Test", "number of characters: " + body.length());
            } while (cursor.moveToNext());
        }

        cursor.close();
        Editor editor = prefs.edit();
        editor.putLong("time_last_checked", timeLastChecked);
        editor.commit();
        return null;
        }

    }
}

请帮我找到解决方案。

4

1 回答 1

2

我有同样的问题,它只出现在某些设备上(或者我以前总是有一些数据)。

问题是,当您将结果过滤到没有数据的时段(例如,您只想要最后一天的 SMS 而用户没有发送任何数据)时,您会得到光标。我觉得这很奇怪,我相信在某些设计上你得到了正确初始化的游标,只是它是空的,但在其他设计上你得到空指针,所以在访问游标之前总是检查空指针!

顺便说一句,“问题已解决”而不提供答案?不帅哥们!

于 2013-11-02T14:37:16.490 回答