gpt4 book ai didi

android - UpdateAppWidget 空指针异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:09:08 26 4
gpt4 key购买 nike

以下是更新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): at android.widget.RemoteViews$ReflectionAction.writeToPa rcel(RemoteViews.java:400) W/System.err( 522): at android.widget.RemoteViews.writeToParcel(RemoteViews. java:1012) W/System.err( 522): at com.android.internal.appwidget.IAppWidgetService$Stub $Proxy.updateAppWidgetIds(IAppWidgetService.java:374) W/System.err( 522): at android.appwidget.AppWidgetManager.updateAppWidget(Ap pWidgetManager.java:246) W/System.err( 522): at android.appwidget.AppWidgetManager.updateAppWidget(Ap pWidgetManager.java:265) W/System.err( 522): at com.mobisys.android.app_widget.MyWidgetProvider.updat eAppWidget(MyWidgetProvider.java:185) W/System.err( 522): at com.mobisys.android.app_widget.MyWidgetProvider.onRec eive(MyWidgetProvider.java:71) W/System.err( 522): at android.app.ActivityThread.handleReceiver(ActivityThr ead.java:1769) W/System.err( 522): at android.app.ActivityThread.access$2400(ActivityThread .java:117) W/System.err( 522): at android.app.ActivityThread$H.handleMessage(ActivityTh read.java:978) W/System.err( 522): at android.os.Handler.dispatchMessage(Handler.java:99) W/System.err( 522): at android.os.Looper.loop(Looper.java:123) W/System.err( 522): at android.app.ActivityThread.main(ActivityThread.java:3 647) W/System.err( 522): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err( 522): at java.lang.reflect.Method.invoke(Method.java:507) W/System.err( 522): at com.android.internal.os.ZygoteInit$MethodAndArgsCalle r.run(ZygoteInit.java:839) W/System.err( 522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.ja va:597) W/System.err( 522): at dalvik.system.NativeStart.main(Native Method)

我正在以下列方式从挂起的 Activity 中调用 updateAppWidget 静态方法:

MyWidgetProvider.updateAppWidget(context, mAppWidgetId, post); //context is pending Activity's instance

可能是什么问题?

提前致谢。

最佳答案

我有完全相同的问题和堆栈跟踪。原因是我在 setImageViewBitmap 中传递的空位图。现在回答可能为时已晚,但我认为你应该仔细检查这一行:

views.setImageViewBitmap(R.id.actor_photo, post.image);

如果我说得对,post.image 将为 null - 您应该删除此行或提供非 null 位图。

问候

关于android - UpdateAppWidget 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com