问题:应用程序有时会在启动时抛出异常,但并非总是如此。我不明白为什么有时会发生这种情况,但有时不会。任何建议将不胜感激。
一些日志:
04-18 17:58:12.437: E/AndroidRuntime(26208): FATAL EXCEPTION: main
04-18 17:58:12.437: E/AndroidRuntime(26208): java.lang.RuntimeException: Unable to start receiver com.cm.clou.ContactsListUpdateReceiver: java.lang.NullPointerException
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.os.Looper.loop(Looper.java:130)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-18 17:58:12.437: E/AndroidRuntime(26208): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 17:58:12.437: E/AndroidRuntime(26208): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 17:58:12.437: E/AndroidRuntime(26208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-18 17:58:12.437: E/AndroidRuntime(26208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-18 17:58:12.437: E/AndroidRuntime(26208): at dalvik.system.NativeStart.main(Native Method)
04-18 17:58:12.437: E/AndroidRuntime(26208): Caused by: java.lang.NullPointerException
04-18 17:58:12.437: E/AndroidRuntime(26208): at com.cm.clou.ConversationsListActivity.notifyAdaper(ConversationsListActivity.java:270)
04-18 17:58:12.437: E/AndroidRuntime(26208): at com.cm.clou.ContactsListUpdateReceiver.onReceive(ContactsListUpdateReceiver.java:17)
04-18 17:58:12.437: E/AndroidRuntime(26208): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
收货人代码:
public class ContactsListUpdateReceiver extends BroadcastReceiver {
public static final String TAG = ContactsListUpdateReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
if (message != null) {
Log.d(TAG, "Uslishali otvet ot servisa po apdejtu contactov: " + message);
ConversationsListActivity.notifyAdaper(context);
}
}
}
在清单中:
<receiver
android:name="com.cm.clou.ContactsListUpdateReceiver"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="com.cm.clou.ACTION_CONTACTS_LIST_UPDATE" />
</intent-filter>
</receiver>
还有我的 notifyAdaper 代码:
private static ConversationsListCursorAdapter conversationsListCursorAdapter = null;
@SuppressLint("NewApi")
public static void notifyAdaper(Context context) {
Uri uri = ClouContentProvider.CONVERSATIONS_CONTENT_URI;
Cursor cursor = null;
if (android.os.Build.VERSION.SDK_INT < 11) {
cursor = context.getContentResolver().query(uri, null, null, null, null);
} else {
CursorLoader cursorLoader = new CursorLoader(context, uri, null, null, null, null);
cursor = cursorLoader.loadInBackground();
}
conversationsListCursorAdapter.changeCursor(cursor);
conversationsListCursorAdapter.notifyDataSetChanged();
}