Android:小部件 - 在远程视图上使用 setOnclickPendingIntent 时出现空指针异常?

发布于 2024-11-08 14:35:28 字数 3314 浏览 2 评论 0原文

我有一个小部件(appwidgetprovider + 服务) 该服务基本上像这样更新 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(widget,views)有关;)

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不为空)小部件、视图)指令。 那么我在设置pendingIntent时做错了什么???

任何帮助将不胜感激! 光盘。

工作解决方案(根据帖子作者):

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); 
}

I got a Widget ( appwidgetprovider + service)
This service updates the UI basically like this :

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);

So Everything works fine without using my static method that set PendingIntent to id,
setPendingIntentForAllWidgetSubView
Here is the code :

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);
}

If I use it , here is the NPE I Got : ( line 283 is related to manager.updateAppWidget(widget, views); )

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)

Furthermore, I'm sure widget and views are not null in manager.updateAppWidget(widget, views) instruction.
So What Am I doing Wrong when setting pendingIntent ????

Any Helps would be appreciate !!!
CD.

Working solution (according to post author):

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); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-11-15 14:35:28

尝试使用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);
}

让我知道这是否有效。

Try using 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);
}

Let me know if that works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文