2013-05-10 61 views
1

我知道這已經被問過太多次了,但答案似乎並不適用。用getExtras()返回null

活性1:

public void buttonPress(View view){ 
    Intent i = new Intent(this,ProfilePage.class); 
    i.putExtra("USER_DETAILS", UD); 
    startActivity(i); 
} 

活動2:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_profile_page); 
    try{ 
     UserDetails UD = (UserDetails)this.getIntent().getExtras().getParcelable("USER_DETAILS"); 
     ((TextView)findViewById(R.id.First)).setText(UD.getFirst_name()); 
     ((TextView)findViewById(R.id.Last)).setText(UD.getLast_name()); 
    }catch (Exception e) { 
     Log.e("GETTING EXTRAS", e.toString()); 
    } 
} 

「UD」 是parcelable因爲我有它正確地返回別處。 this.getIntent()。getExtras()。getParcelable(「USER_DETAILS」)簡單地返回null。 我繼續遇到這個問題,我該如何修復它,或者我根本沒有得到什麼?

+1

您能打印異常嗎?在logcat中粘貼logcat :) – 2013-05-10 03:25:09

+0

當訪問UD的方法時它是一個'nullPointException',但問題應該立即解決!謝謝! – 2013-05-10 03:47:19

+0

如果您的問題已解決,請將解決方案作爲答案添加並接受。 – Mudassir 2013-05-10 05:03:36

回答

0

您可以通過製作的UserDetails實現Serializable做這樣的事情:

Intent i = new Intent(this,ProfilePage.class); 
i.putExtra("USER_DETAILS", UD); 

,然後檢索這樣的對象:

UserDetails UD = (UserDetails)this.getIntent().getSerializableExtra("USER_DETAILS"); 

編輯: 在性能上重要的序列化慢於Parcelable,Check這個出來。無論如何,我發佈這個解決方案,以解決您的問題。

+0

反正我不需要'實現'可序列化。我也聽說它比較慢。如果我錯了,請讓我陷入困境! – 2013-05-10 03:45:20

1

嘗試

UserDetails UD = (UserDetails) getIntent().getParcelableExtra("USER_DETAILS"); 
+0

好吧,我想出了我的問題,(我也有一個名爲UD的領域)< - 白癡,但我想知道是否有太多的額外獲取方法之間的區別? – 2013-05-10 03:43:13

+1

根據Android Intent文檔,getExtra()會返回您使用putExtra()添加的所有附加功能的地圖。爲了明確地獲取您的parcelable對象,您必須使用getParcelableExtra()。 – ps2010 2013-05-11 03:06:52

0

也許this可以幫助您與Parcelable

如果您是通過intent發送non-primitive type data/Object到另一個活動,你必須要麼Serialize或實現Parcelable該對象。首選技術是Parcelable,因爲它不影響性能。

很多人會告訴你,Serialization是非常慢,效率低下,這是正確的。但是,作爲計算機程序員你永遠不想做的一件事就是對性能作出絕對的評論。

問問自己,如果serialization正在放慢你的程序。你注意到它從活動到活動的時間?你注意到它何時保存/加載?如果不是,那很好。當你使用大量的手動序列化代碼時,你不會佔用更小的空間,所以這裏沒有優勢。所以what if it is 100 times slower than an alternative if 100 times slower means 10ms instead of 0.1ms?你不會看到,所以誰在乎?而且,爲什麼有人會在編寫手動序列化時投入大量精力,以免在性能上產生任何可察覺的差異?

0

我剛開發了我的第一個實現接口Parcelable的Parcelable類。 我真的很開心,因爲它能正常工作。 也許我的解決方案可以幫助任何人:

public class DealCategory implements Parcelable { 

private int categoryID; 
private String categoryName; 
private List<DealCategory> listaCategoriasSeleccionadas = new ArrayList<DealCategory>(); 

/** 
* GET/SET 
*/ 



//-----------------------------------------------------------| 
//-----------------------------------------------------------| 
//------------------- METHODS FOR PARCELABLE ----------------| 
//-----------------------------------------------------------| 
//-----------------------------------------------------------| 

/* 
* (non-Javadoc) 
* @see android.os.Parcelable#describeContents() 
* Implementacion de los metodos de la Interfaz Parcelable 
*/ 
@Override 
public int describeContents() { 
    return hashCode(); 
} 

/* 
* (non-Javadoc) 
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int) 
* IMPORTANT 
* We have to use the same order both TO WRITE and TO READ 
*/ 
@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(categoryID); 
    dest.writeString(categoryName); 
    dest.writeTypedList(listaCategoriasSeleccionadas); 
} 


/* 
* (non-Javadoc) 
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int) 
* IMPORTANT 
* We have to use the same order both TO WRITE and TO READ 
* 
* We reconstruct the object reading from the Parcel data 
*/ 
public DealCategory(Parcel p) { 
    categoryID = p.readInt();  
    categoryName = p.readString(); 
    p.readTypedList(listaCategoriasSeleccionadas, DealCategory.CREATOR);  
} 


/* 
* (non-Javadoc) 
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int) 
* We need to add a Creator 
*/ 
public static final Parcelable.Creator<DealCategory> CREATOR = new Parcelable.Creator<DealCategory>() { 

    @Override  
    public DealCategory createFromParcel(Parcel parcel) { 
     return new DealCategory(parcel); 
    } 

    @Override  
    public DealCategory[] newArray(int size) {  
     return new DealCategory[size]; 
    }  
}; 

} 

我送(寫)對象Parcelable 「DealCategory」 從活動A到活動B

protected void returnParams(DealCategory dc) { 
     Intent intent = new Intent(); 
     intent.putExtra("Category", dc); 
     setResult(REQUEST_CODE_LISTA_DEALS, intent); 
     finish() 
} 

我接受(讀取)對象Parcelable 「DealCategory」來自活動A的活動B

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    Bundle b = data.getExtras();    
    DealCategory dc = (DealCategory) b.getParcelable("Category"); 

我檢查是否收到更正的值。我temporaly顯示他們在日誌

for (int i = 0; i < dc.getListaCategorias().size(); i++) { 
      Log.d("Selected Category", "ID: " + dc.getListaCategorias().get(i).getCategoryID() + " -- NAME:" + dc.getListaCategorias().get(i).getCategoryName()); 
      lR += dc.getListaCategorias().get(i).getCategoryName() +", "; 
     } 

} //Close onActivityResult 
0
UserDetails UD 

被宣佈爲既是一個字段和局部變量,我沒有注意到,

我想這個問題的一個很好的副產品是...

getIntent().getParcelableExtra("USER_DETAILS"); 
getIntent().getExtras().getParcelable("USER_DETAILS"); 

...工作方式不同,需要保持一致的方法。