簡體   English   中英

通過意圖傳遞數據不起作用

[英]Passing data through intent not working

更新資料

最后,正如評論中指出的那樣,這是一個序列化問題。


因此,我在適配器的ViewHolder中有此方法:

@Override
public void onClick(View view) {
    int position = getAdapterPosition();
    if (position != RecyclerView.NO_POSITION) {
        Offer offer = offers.get(position);
        Intent intent = new Intent(context, MyOfferActivity.class);
        Bundle extras = new Bundle();
        extras.putSerializable(Constants.OFFER_INTENT_KEY, offer);
        extras.putSerializable(Constants.USER_INTENT_KEY, user);
        intent.putExtras(extras);
        context.startActivity(intent);
    }
}

然后在MyOfferActivityonCreateView上執行以下操作:

Bundle extras = getIntent().getExtras();
offer = (Offer) extras.getSerializable(Constants.OFFER_INTENT_KEY);
user = (User) extras.getSerializable(Constants.USER_INTENT_KEY);

問題是offer最終為null 我已經調試了它,當我將其放入捆綁包中時,它不是null。 我可以讓user很好。

以前,此操作沒有任何問題,並且“神奇地”停止了工作。

實際上,最初我是這樣做的,並且由於它從頭開始停止工作,因此將其更改為以前的代碼,以查看是否可以解決該問題。 沒有。

@Override
public void onClick(View view) {
    int position = getAdapterPosition();
    if (position != RecyclerView.NO_POSITION) {
        Offer offer = offers.get(position);
        Intent intent = new Intent(context, MyOfferActivity.class);
        intent.putExtra(Constants.USER_INTENT_KEY, user);
        intent.putExtra(Constants.OFFER_INTENT_KEY, offer);
        context.startActivity(intent);
    }
}

並在接收器上:

offer = (Offer) getIntent().getSerializableExtra(Constants.OFFER_INTENT_KEY);
user = (User) getIntent().getSerializableExtra(Constants.USER_INTENT_KEY); 

會是什么

我嘗試了這兩個版本的代碼,還更改了我設置額外內容並接收它們的順序,但仍然無法正常工作。 另外, getIntent().hasExtra(Constants.OFFER_INTENT_KEY)返回false。

這是錯誤:

FATAL EXCEPTION: main
   Process: com.yournanny, PID: 742
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yournanny/com.yournanny.activities.offers.MyOfferActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.yournanny.models.Nanny com.yournanny.models.Offer.getAcceptedNanny()' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:241)
       at android.app.ActivityThread.main(ActivityThread.java:6223)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.yournanny.models.Nanny com.yournanny.models.Offer.getAcceptedNanny()' on a null object reference
       at com.yournanny.activities.offers.MyOfferActivity.setContent(MyOfferActivity.java:69)
       at com.yournanny.activities.offers.MyOfferActivity.onCreate(MyOfferActivity.java:64)
       at android.app.Activity.performCreate(Activity.java:6705)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772) 
       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:241) 
       at android.app.ActivityThread.main(ActivityThread.java:6223) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

導致它的行是這樣的: if (user.getType() == UserType.FAMILY && offer.getAcceptedNanny() == null && offer.getState() == State.REQUESTED)這是我使用的第一個offer活動中的內容。

您的第二個選項應該可以正常工作。

更換

extras.putSerializable(Constants.OFFER_INTENT_KEY, offer);
extras.putSerializable(Constants.USER_INTENT_KEY, user);

intent.putExtra(Constants.OFFER_INTENT_KEY, offer);
intent.putExtra(Constants.USER_INTENT_KEY, user);

而關於你的活動

offer = (Offer) getIntent().getSerializableExtra(Constants.OFFER_INTENT_KEY);
user = (User) getIntent().getSerializableExtra(Constants.USER_INTENT_KEY);

您好@moondaisy,我建議您使用Parcelable接口傳遞數據。 這是鏈接https://developer.android.com/reference/android/os/Parcelable.html還要檢查一下如何使我的自定義對象可拆分?

可打包界面比Serilization更快,可提高您的應用效率。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM