Android:Widget - Null指针在远程视图上使用setOnclickPendingIntent时出现异常?

时间:2011-05-19 14:24:24

标签: android android-widget android-appwidget android-pendingintent

我有一个小工具(appwidgetprovider + service) 此服务基本上更新UI:

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)

此外,我确信在manager.updateAppWidget(widget,views)指令中widget和视图不为null。 那么在设置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); 
}

1 个答案:

答案 0 :(得分: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);
}

如果有效,请告诉我。