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服务成功更新。当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(AppWidgetManager.java:246) W/System.err(522): 在android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:265) W/System.err(522): 在com.mobisys.android.app_widget.MyWidgetProvider.updateAppWidget(MyWidgetProvider.java:185) W/System.err(522): 在com.mobisys.android.app_widget.MyWidgetProvider.onReceive(MyWidgetProvider.java:71) W/System.err(522): 在android.app.ActivityThread.handleReceiver(ActivityThread.java:1769) W/System.err(522): 在android.app.ActivityThread.access$2400(ActivityThread.java:117) W/System.err(522): 在android.app.ActivityThread$H.handleMessage(ActivityThread.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:3647) 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$MethodAndArgsCaller.run(ZygoteInit.java:839) W/System.err(522): 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) W/System.err(522): 在dalvik.system.NativeStart.main(Native Method)
我是通过以下方式从Pending Activity调用updateAppWidget静态方法的:
MyWidgetProvider.updateAppWidget(context, mAppWidgetId, post); //context is pending Activity's instance
什么可能是问题?
提前感谢你。