从 stackview 小部件传递可序列化对象会产生空指针异常

Posted

技术标签:

【中文标题】从 stackview 小部件传递可序列化对象会产生空指针异常【英文标题】:passing serializable object from stackview widget gives null pointer exception 【发布时间】:2015-05-10 05:09:51 【问题描述】:

AppWidget 提供者(BroadcastReciever)

(尝试从 Stackview Widget 发送可序列化对象) 当我通过pendingIntent发送int作为额外时我能够在活动中接收但是当我尝试发送可序列化的额外时它给了我空指针异常 提前致谢。

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 

for (int i = 0; i < appWidgetIds.length; ++i) 
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_profile_stackview);

// set intent for widget service that will create the views
Intent serviceIntent = new Intent(context,WidgetProfieService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);


serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); 


 remoteViews.setRemoteAdapter( R.id.widget_profilestackimageview, serviceIntent);


            Intent viewIntent = new Intent(context, ProfileActivity.class);
            viewIntent.setAction(Intent.ACTION_VIEW);
            viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            viewIntent.setData(Uri.parse(viewIntent.toUri(Intent.URI_INTENT_SCHEME)));

            PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
            remoteViews.setPendingIntentTemplate(R.id.widget_profilestackimageview, viewPendingIntent);

            // update widget
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    

RemoteViewService(发送额外的int(图像))

public RemoteViews getViewAt(int position) 


   RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_profile);
rv.setImageViewResource(R.id.stack_profile_pic, ((Profile) mWidgetProfiles.get(position)).getImageId());
Log.d("profile",mWidgetProfiles.get(position).getCountry());

Log.d("profile",mWidgetProfiles.get(position).getImageId()+"");

Bundle extras = new Bundle();

// extras.putInt("profile",mWidgetProfiles.get(position).getImageId()); //使用int额外的工作

  extras.putSerializable("profile",(Profile)mWidgetProfiles.get(position));
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.profile_widgetlayout, fillInIntent);



try 
    System.out.println("Loading view " + position);
    Thread.sleep(1000);
 catch (InterruptedException e) 
    e.printStackTrace();

return rv;

接收活动

protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_profile);

/* 在这里使用 ExtraInt 有效 ImageView img=(ImageView) findViewById(R.id.activity_profilepic);

    if(getIntent().getIntExtra("profile",0)!=0)
        Log.d("widgetpic",getIntent().getIntExtra("profile",0)+"");
        img.setImageResource(getIntent().getIntExtra("profile",0));
    

*/

ImageView img=(ImageView) findViewById(R.id.activity_profilepic);
        Bundle extras = getIntent().getExtras();

        if(extras !=null)
            if(extras.containsKey("profile"))
            
            Profile mProfile= (Profile)extras.getSerializable("profile");
            img.setImageResource(mProfile.getImageId());
        

    

【问题讨论】:

还是一样!! 【参考方案1】:

从 stackview 小部件传递可序列化对象给出空指针 异常

因为用 bundle 发送 profile 密钥,但试图从 getIntent() 返回的 Intent 中获取 profile。这样做:

Bundle extras = getIntent().getExtras();

    if(extras !=null)
      if(extras.containsKey("profile"))
        
           Profile mProfile= (Profile)extras.getSerializable("profile"); 
        

【讨论】:

嘿,感谢您的回复,但我之前也尝试过它仍然显示 null 并跳过如果它无法找到“个人资料”

以上是关于从 stackview 小部件传递可序列化对象会产生空指针异常的主要内容,如果未能解决你的问题,请参考以下文章

QDockWidget - QGlWidget - 当小部件从停靠栏中拖出时,可绘制无效

使用可序列化将对象从活动传递到活动的正确方法?

如何在颤动中将数据从一个小部件传递到另一个小部件?

使用继承的小部件传递小部件键

使用android中的可序列化将对象从第二个活动传递回主要活动

将数据从 Flutter Stream 传递到小部件