問題: アプリケーションが起動時に例外をスローすることがありますが、常にではありません。なぜこれが起こるのか理解できませんが、そうでない場合もあります。どんな提案でも大歓迎です。
いくつかのログ:
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();
}