以下是更新 appwidget 的静态方法:
public static void updateAppWidget(Context context, int id, WALL_POST_DETAILS post){
try{
System.out.println("In updateAppWidget==>"+id);
AppWidgetManager mngr=AppWidgetManager.getInstance(context);
String diff=diff(post.created_time.trim());
RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
PendingIntent pending=PendingIntent.getActivity(context, 0, new Intent(context,ShareDialogActivity.class), 0);
views.setOnClickPendingIntent(R.id.TextView01, pending);
views.setOnClickPendingIntent(R.id.Button03, pending);
views.setTextViewText(R.id.actor_name, post.actor_name);
views.setTextViewText(R.id.actor_message, post.message);
views.setImageViewBitmap(R.id.actor_photo, post.image);
views.setTextViewText(R.id.created_time, diff);
if(post.no_of_comments>0){
views.setViewVisibility(R.id.comments_image, View.VISIBLE);
views.setViewVisibility(R.id.comment_word, View.VISIBLE);
views.setTextViewText(R.id.no_of_comments, String.valueOf(post.no_of_comments));
views.setViewVisibility(R.id.no_of_comments, View.VISIBLE);
System.out.println("No. of Comments==>"+post.no_of_comments);
}else{
views.setViewVisibility(R.id.comments_image, View.INVISIBLE);
views.setViewVisibility(R.id.comment_word, View.INVISIBLE);
views.setTextViewText(R.id.no_of_comments, String.valueOf(""));
views.setViewVisibility(R.id.no_of_comments, View.INVISIBLE);
}
if(post.no_of_likes>0){
views.setViewVisibility(R.id.likes_image, View.VISIBLE);
views.setViewVisibility(R.id.likes_word, View.VISIBLE);
views.setTextViewText(R.id.no_of_likes, String.valueOf(post.no_of_likes));
views.setViewVisibility(R.id.no_of_likes, View.VISIBLE);
}else{
views.setViewVisibility(R.id.likes_image, View.INVISIBLE);
views.setViewVisibility(R.id.likes_word, View.INVISIBLE);
views.setTextViewText(R.id.no_of_likes, String.valueOf(""));
views.setViewVisibility(R.id.no_of_likes, View.INVISIBLE);
}
Intent intent_next=new Intent(context,GetPostService.class);
Bundle b_next=new Bundle();
b_next.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
b_next.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.NEXT_FB_POST);
intent_next.putExtras(b_next);
PendingIntent nextPostService=PendingIntent.getService(context, MyWidgetProvider.NEXT_FB_POST, intent_next, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.next_button, nextPostService);
Intent intent_prev=new Intent(context,GetPostService.class);
Bundle b_prev=new Bundle();
b_prev.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
b_prev.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.PREV_FB_POST);
intent_prev.putExtras(b_prev);
PendingIntent prevPostService=PendingIntent.getService(context, MyWidgetProvider.PREV_FB_POST, intent_prev, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.prev_button, prevPostService);
Intent intent_comment=new Intent(context,FBShowCommentsActivity.class);
Bundle b_comment=new Bundle();
b_comment.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
b_comment.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.SHOW_COMMENTS);
b_comment.putString(MyWidgetProvider.POST_ID, SessionStore.restoreCurrentPostId(context));
intent_comment.putExtras(b_comment);
PendingIntent fbActionActivity=PendingIntent.getActivity(context, MyWidgetProvider.SHOW_COMMENTS, intent_comment, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.comment_word, fbActionActivity);
views.setOnClickPendingIntent(R.id.comments_image, fbActionActivity);
views.setOnClickPendingIntent(R.id.no_of_comments, fbActionActivity);
Intent intent_likes=new Intent(context,ShowLikeDialogActivity.class);
Bundle b_likes=new Bundle();
b_likes.putInt(MyWidgetProvider.APP_WIDGET_ID, id);
b_likes.putInt(MyWidgetProvider.REQUEST_CODE, MyWidgetProvider.SHOW_LIKES);
b_likes.putString(MyWidgetProvider.POST_ID, SessionStore.restoreCurrentPostId(context));
intent_likes.putExtras(b_likes);
PendingIntent fbLikesActivity=PendingIntent.getActivity(context, MyWidgetProvider.SHOW_LIKES, intent_likes, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.likes_word, fbLikesActivity);
views.setOnClickPendingIntent(R.id.likes_image, fbLikesActivity);
views.setOnClickPendingIntent(R.id.no_of_likes, fbLikesActivity);
mngr.updateAppWidget(id, views);
}catch(Exception e){
e.printStackTrace();
System.out.println("Exception==>"+e.getMessage());
}
}
AppWidget 正在从 GetPostService 挂起服务成功更新。安装后第一次启动 Pending Activity 时,我可以成功更新小部件。但是如果我再次启动第二次,Activity 会正确启动,但 AppWidget 没有得到更新,它会抛出空指针异常。我正在从 UI 线程(从 AsyncTask 的 onPostExecute() 方法)更新小部件。
W/System.err(522): 在 android.widget.RemoteViews$ReflectionAction.writeToParcel(RemoteViews.java:400) W/System.err(522): 在 android.widget.RemoteViews.writeToParcel(RemoteViews.java:1012 ) W/System.err(522): 在 com.android.internal.appwidget.IAppWidgetService$Stub $Proxy.updateAppWidgetIds(IAppWidgetService.java:374) W/System.err(522): 在 android.appwidget.AppWidgetManager.updateAppWidget (App pWidgetManager.java:246) W/System.err(522): 在 android.appwidget.AppWidgetManager.updateAppWidget(App pWidgetManager.java:265) W/System.err(522): 在 com.mobisys.android.app_widget .MyWidgetProvider.updat eAppWidget(MyWidgetProvider.java:185) W/System.err(522):在 com.mobisys.android.app_widget.MyWidgetProvider.onRec eive(MyWidgetProvider.java:71) W/System.err(522):在 android.app.ActivityThread.handleReceiver(ActivityThr ead.java:1769) W/System。错误(522):在 android.app.ActivityThread.access$2400(ActivityThread.java:117)W/System.err(522):在 android.app.ActivityThread$H.handleMessage(ActivityTh read.java:978)W/ System.err(522):在 android.os.Handler.dispatchMessage(Handler.java:99)W/System.err(522):在 android.os.Looper.loop(Looper.java:123)W/System。错误(522):在 android.app.ActivityThread.main(ActivityThread.java:3 647)W/System.err(522):在 java.lang.reflect.Method.invokeNative(本机方法)W/System.err( 522): 在 java.lang.reflect.Method.invoke(Method.java:507) W/System.err(522): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839 ) W/System.err(522): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522): 在 dalvik.system.NativeStart.main(Native Method )java:117) W/System.err(522): 在 android.app.ActivityThread$H.handleMessage(ActivityTh read.java:978) W/System.err(522): 在 android.os.Handler.dispatchMessage(Handler .java:99) W/System.err(522): 在 android.os.Looper.loop(Looper.java:123) W/System.err(522): 在 android.app.ActivityThread.main(ActivityThread.java :3 647) W/System.err(522): 在 java.lang.reflect.Method.invokeNative(Native Method) W/System.err(522): 在 java.lang.reflect.Method.invoke(Method.java :507) W/System.err(522): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522): 在 com.android.internal。 os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522): at dalvik.system.NativeStart.main(Native Method)java:117) W/System.err(522): 在 android.app.ActivityThread$H.handleMessage(ActivityTh read.java:978) W/System.err(522): 在 android.os.Handler.dispatchMessage(Handler .java:99) W/System.err(522): 在 android.os.Looper.loop(Looper.java:123) W/System.err(522): 在 android.app.ActivityThread.main(ActivityThread.java :3 647) W/System.err(522): 在 java.lang.reflect.Method.invokeNative(Native Method) W/System.err(522): 在 java.lang.reflect.Method.invoke(Method.java :507) W/System.err(522): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522): 在 com.android.internal。 os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522): at dalvik.system.NativeStart.main(Native Method)os.Handler.dispatchMessage(Handler.java:99) W/System.err(522):在 android.os.Looper.loop(Looper.java:123) W/System.err(522):在 android.app。 ActivityThread.main(ActivityThread.java:3 647) W/System.err(522):在 java.lang.reflect.Method.invokeNative(Native Method) W/System.err(522):在 java.lang.reflect。 Method.invoke(Method.java:507) W/System.err(522): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522): 在 dalvik.system.NativeStart.main(本机方法)os.Handler.dispatchMessage(Handler.java:99) W/System.err(522):在 android.os.Looper.loop(Looper.java:123) W/System.err(522):在 android.app。 ActivityThread.main(ActivityThread.java:3 647) W/System.err(522):在 java.lang.reflect.Method.invokeNative(Native Method) W/System.err(522):在 java.lang.reflect。 Method.invoke(Method.java:507) W/System.err(522): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522): 在 dalvik.system.NativeStart.main(本机方法)invokeNative(本机方法)W/System.err(522):在 java.lang.reflect.Method.invoke(Method.java:507)W/System.err(522):在 com.android.internal.os.ZygoteInit $MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522):在 dalvik.system.NativeStart.main(本机方法)invokeNative(本机方法)W/System.err(522):在 java.lang.reflect.Method.invoke(Method.java:507)W/System.err(522):在 com.android.internal.os.ZygoteInit $MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err(522): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err(522):在 dalvik.system.NativeStart.main(本机方法)
我从 Pending Activity 以下列方式调用 updateAppWidget 静态方法:
MyWidgetProvider.updateAppWidget(context, mAppWidgetId, post); //context is pending Activity's instance
可能是什么问题?
提前致谢。