3

我有一個小工具(的AppWidgetProvider +服務) 這個服務更新UI基本上是這樣的:Android:小部件 - 空指針在remoteviews上使用setOnclickPendingIntent時出現異常?

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 
//doing some stuff like which works without any errors : 
views.setTextViewText(R.id.Time, time); 

// Here I'm calling a static method that set pendingIntent on different ID : 
// With it : Null Pointer Exception, without not. 
views = HomeWidgetUtils.setPendingIntentForAllWidgetSubView(context, views); 

//update AppWidgetProvider : 
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
ComponentName thisAppWidget = new ComponentName(context, WDWidgetProvider.class); 
manager.updateAppWidget(widget, views); 

所以一切都沒有用我那設置的PendingIntent到ID靜態方法, setPendingIntentForAllWidgetSubView 這裏工作得很好是代碼:

public static RemoteViews setPendingIntentForAllWidgetSubView(Context context,RemoteViews rv){ 
    rv.setOnClickPendingIntent(R.id.Widget, HomeWidgetUtils.getPendingIntentonActivity(context, WeatherForecast.class)); 

rv.setOnClickPendingIntent(R.id.globallayout, HomeWidgetUtils.getPendingIntentonActivity(context, TabActivity01.class)); 
    return rv; 
} 

public static PendingIntent getPendingIntentonActivity(Context context,Class C) { 
    Intent intentleft = new Intent(context, C); 
    intentleft.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    return PendingIntent.getActivity(context, 1, intentleft,PendingIntent.FLAG_UPDATE_CURRENT); 
} 

如果我使用它,這裏就是NPE我得到:(283線有關manager.updateAppWidget(窗口小部件,意見);)

java.lang.NullPointerException 
at android.widget.RemoteViews$SetOnClickPendingIntent.writeToParcel(RemoteViews.java:131) 
at android.widget.RemoteViews.writeToParcel(RemoteViews.java:1003) 
at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetIds(IAppWidgetService.java:374) 
at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:246) 
at fr.cdcorp.homewidget.clock.ClockWidgetService.update(ClockWidgetService.java:283) 
at fr.cdcorp.homewidget.clock.ClockWidgetService$1.onReceive(ClockWidgetService.java:312) 
at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 

此外,我確定widget和視圖在manager.updateAppWidget(widget,views)指令中不爲空。 那麼當我設置pendingIntent時我在做什麼錯?

任何幫助將被讚賞! CD。

工作液(根據文章作者):

public static PendingIntent getPendingIntentonActivity(Context context,Class C) { 
    PendingIntent mPendingIntent = null; 
    Intent mIntent = new Intent(Intent.ACTION_MAIN).addCategory(
     Intent.CATEGORY_LAUNCHER); 
    ComponentName cn = new ComponentName(context, C); 
    mIntent.setComponent(cn); 
    return PendingIntent.getActivity(context, 0, mIntent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 
} 
+0

檢查您是否在AndroidManifest.xml中聲明瞭您的WeatherForecast.class和TabActivity01.class – doNotCheckMyBlog 2011-05-19 14:37:27

回答

0

嘗試使用FLAG_ACTIVITY_NEW_TASK

public static PendingIntent getPendingIntentonActivity(Context context,Class C) { 
    Intent intentleft = new Intent(context, C); 
    intentleft.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    intentleft.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    return PendingIntent.getActivity(context, 1, intentleft,PendingIntent.FLAG_UPDATE_CURRENT); 
} 

讓我知道是否可行。

+0

謝謝,但沒有任何更改。 NPE再次......真的不知道它爲什麼失敗。 – CDCorp 2011-05-19 14:43:54

+0

這是哪個android版本? – Femi 2011-05-19 15:00:44

+0

2.2 apilevel 8,但似乎很多用戶用我的最新更新得到這個。真奇怪的是,這就像HTC Desire 2.2上的魅力一樣......所以我可以重現仿真器2.2 api level 8 ..以及其他api level的問題。 – CDCorp 2011-05-19 15:04:40

相關問題