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

时间:2015-03-09 04:36:27

标签: android android-intent android-activity android-widget android-pendingintent

AppWidget Provider(BroadcastReciever)

(尝试从Stackview Widget发送Serializable对象) 当我发送int作为额外虽然​​pendingIntent我能够在活动中收到,但当我尝试发送可序列化的额外它给我空指针异常 提前谢谢。

@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( “轮廓”,mWidgetProfiles.get(位置).getImageId());
  //使用int extra works

  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 个答案:

答案 0 :(得分:0)

  

从stackview小部件传递可序列化对象会给出空指针   例外

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

Bundle extras = getIntent().getExtras();

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