簡體   English   中英

在Android中通過意圖傳遞捆綁時在Main活動上獲取捆綁

[英]Getting bundle on Main activity when passing bundles by intent in android

所以我遇到的問題是我的應用在啟動時一直崩潰,我有兩個活動。 活動A和活動B。我的應用程序在活動A上啟動,但是我在活動B中創建了一個包,並將其發送給活動A。因此,當它啟動時,該包為空或為null從而崩潰了,我該如何解決? 謝謝。

這是在活動A(啟動活動)中創建的

    Bundle extras = getIntent().getExtras();
    Title = extras.getString("Title");
    Description = extras.getString("Description");
    Price = extras.getString("Price");
    Availability = extras.getString("Availability");

然后讓我在活動B中創建捆綁包

     Intent intent = new Intent(B.this, A.class);
                Bundle extras = new Bundle();
                extras.putString("Title", PostTitle);
                extras.putString("Description", PostDescription);
                extras.putString("Price", PostPrice);
                extras.putString("Availability", PostAvail);
                intent.putExtras(extras);
                startActivity(intent);

我建議以下內容:

A.從意圖使用捆綁包:

Intent pIntent = new Intent(this, JustaClass.class);
Bundle extras = pIntent.getExtras();
extras.putString(key, value); 

B.創建一個新的捆綁包:

Intent pIntent = new Intent(this, JustaClass.class);
Bundle pBundle = new Bundle();
pBundle.putString(key, value);
mIntent.putExtras(pBundle);

C.使用Intent的putExtra()方法:

Intent pIntent = new Intent(this, JustaClass.class);
pIntent.putExtra(key, value);

最后,在啟動的活動中,您可以通讀它們:

String value = getIntent().getExtras().getString(key)

我只是使用Strings作為傳遞的示例,我指的是BundleIntent

暫無
暫無

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

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