Android:小部件 - 在远程视图上使用 setOnclickPendingIntent 时出现空指针异常?
我有一个小部件(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
FLAG_ACTIVITY_NEW_TASK
:让我知道这是否有效。
Try using
FLAG_ACTIVITY_NEW_TASK
:Let me know if that works.