我正在嘗試將活動的ArrayList傳遞給Widget,但是我的應用會在我的Widget提供者處粉碎。如何將Arraylist從活動傳遞到Widget
我給了下面的細節。
這裏是我的第一個活動的部分代碼:
Intent intent1 = new Intent(MainActivity.this,
AppProviderWidget.class);
intent1.setAction("android.appwidget.action.APPWIDGET_UPDATE");
// Use an array and EXTRA_APPWIDGET_IDS instead of
// AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = widgetIDs;
intent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
intent1.putExtra("widgetlist", result);
sendBroadcast(intent1);
我發送的數據是在這裏:從AppProviderWidget的onReceive方法
intent1.putExtra("widgetlist", result);
和代碼
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
ArrayList<DuyuruHaber> arl = new ArrayList<DuyuruHaber>();
arl = (ArrayList<DuyuruHaber>) intent
.getSerializableExtra("widgetlist");
try {
liste = arl;
} catch (Exception e) {
Log.w("Log",e.toString());
}}
AppProviderWidget.onReceive(AppProviderWidget.java:170)
arl = (ArrayList<DuyuruHaber>) intent
.getSerializableExtra("widgetlist");
我的日誌貓
08-23 16:28:01.684: E/AndroidRuntime(10067): FATAL EXCEPTION: main
08-23 16:28:01.684: E/AndroidRuntime(10067): java.lang.RuntimeException: Unable to start receiver widget.AppProviderWidget: java.lang.NullPointerException
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.os.Looper.loop(Looper.java:123)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 16:28:01.684: E/AndroidRuntime(10067): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 16:28:01.684: E/AndroidRuntime(10067): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 16:28:01.684: E/AndroidRuntime(10067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-23 16:28:01.684: E/AndroidRuntime(10067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-23 16:28:01.684: E/AndroidRuntime(10067): at dalvik.system.NativeStart.main(Native Method)
08-23 16:28:01.684: E/AndroidRuntime(10067): Caused by: java.lang.NullPointerException
08-23 16:28:01.684: E/AndroidRuntime(10067): at widget.AppProviderWidget.onReceive(AppProviderWidget.java:170)
08-23 16:28:01.684: E/AndroidRuntime(10067): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
感謝您的幫助。
謝謝我不想使用靜態方式,但我認爲需要使用。 –