运行 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;
}
}
}
请帮我找到解决方案。