4

我有一個用於我的應用程序的堆棧小部件,它目前只是在單擊小部件時啓動主要活動。 我想改變這個,所以主要活動被給予關於被點擊的widgetitem的信息。 我想要通過的對象實現Parcelable並可以通過通知中的意圖成功傳遞。然而,當它被綁定在widget的意圖中時,它總是顯示爲空。Android:將對象從收集小部件傳遞到活動

任何想法?

StackWidgetProvider

 // set intent for item click (opens main activity) 
     Intent viewIntent = new Intent(context, DealPadActivity.class); 
     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, PendingIntent.FLAG_UPDATE_CURRENT); 
     rv.setPendingIntentTemplate(R.id.stack_view, viewPendingIntent); 

StackWidgetService

public RemoteViews getViewAt(int position) { 

    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stack_widget_item); 
    if (position < mWidgetItems.size()){ 
     rv.setTextViewText(R.id.stack_widget_title, mWidgetItems.get(position).title); 
     rv.setTextViewText(R.id.stack_widget_price, mWidgetItems.get(position).price); 
     rv.setTextViewText(R.id.stack_widget_heat, mWidgetItems.get(position).heat); 
     Bitmap picture = imageLaoder.getBitmap(mWidgetItems.get(position).imageUrl, true) ; 
     rv.setImageViewBitmap(R.id.stack_widget_image, picture); 
    } 

    Intent fillInIntent = new Intent(); 

    Bundle extras = new Bundle(); 
    extras.putInt(StackWidgetProvider.EXTRA_ITEM, position); 
    extras.putParcelable("notificationDeal", new Deal(...)); 
    fillInIntent.putExtras(extras); 

    rv.setOnClickFillInIntent(R.id.stack_widget_layout, fillInIntent); 
    return rv; 
} 

MainActivity

Bundle data = queryIntent.getExtras(); 

    if (data!=null){ // check to see if from widget or notification 
    Deal deal = data.getParcelable("notificationDeal"); 
      if (deal!=null){ // ****ALWAYS NULL FROM WIDGET**** 
       Log.d(this.getClass().getSimpleName(), "going to show passed deal"); 
       onDealSelected(deal); 
      } 
      int i = data.getInt("com.bencallis.dealpad.stackwidget.EXTRA_ITEM"); 
      Log.d(this.getClass().getSimpleName(), "pressed widget is: " + i); **WIDGET ITEM POSITION AS EXPECTED ** 

** **更新

看來它是與編組(特別是解組)

E/Parcel: Class not found when unmarshalling: com.bencallis.abc.Deal, e: java.lang.ClassNotFoundException: com.bencallis.abc.Deal 

我已導入交易類到StackWidgetService和我試圖設置類加載器,但這樣做的一個問題似乎沒有幫助。

交易比喻正在應用程序的主要活動內成功使用,我只是有這個小部件的問題。

新政類

public Deal(Parcel in) { 
    in.readParcelable(Deal.class.getClassLoader()); 
} 


@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(title); 
    ... 
} 

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
    public Deal createFromParcel(Parcel in) { 
     return new Deal(in); 
    } 

    public Deal[] newArray(int size) { 
     return new Deal[size]; 
    } 
}; 
+0

嗯......這就是你接受同樣的,你發送的意圖是什麼?爲什麼不嘗試對意圖設置一個動作並確認接收到的意圖的動作是否是您發送動作時所採取的動作?我認爲你能得到一個空「交易」的唯一原因是你沒有收到你期望得到的意圖。 –

+0

由於無法找到類,因此解組似乎是個問題。我試圖設置類加載器,但我沒有任何運氣。請參閱更新。 – bencallis

回答

0

你說你已經嘗試設置類加載器。這應該工作。我不確定你是如何設置類加載器的,因爲你沒有提供代碼。試試這個:

MainActivity,插入此:

ClassLoader loader = Deal.class.getClassLoader(); 
queryIntent.setExtrasClassLoader(loader); 

在此之前:

Bundle data = queryIntent.getExtras(); 
相關問題