android – UpdateAppWidget Null Pointer Exception

前端之家收集整理的这篇文章主要介绍了android – UpdateAppWidget Null Pointer Exception前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_1@以下是更新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,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)

我从Pending Activity中按以下方式调用updateAppWidget静态方法

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

可能是什么问题?

提前致谢.

解决方法

我有完全相同的问题和堆栈跟踪.原因是我在setImageViewBitmap中传递了一个null Bitmap.
答案可能为时已晚,但我认为你应该仔细检查这一行:
views.setImageViewBitmap(R.id.actor_photo,post.image);

如果我正确post.image将为null – 您应该删除此行或提供非空位图.

问候

原文链接:https://www.f2er.com/android/318273.html

猜你在找的Android相关文章