2013-08-28 173 views
0

我已經創建了名爲「EventFragment」的listFragment類,我需要在其click事件上傳遞數據對象以傳遞drawerActivity類(片段類),然後我需要將該對象傳遞給另一個名爲EventDisplayActivity類的活動。我可以管理獲取數據對象到drawerActivity類,但我不能將它發送到EventDisplayActivity類。將數據對象傳遞給另一活動的活動(片段 - >其活動 - >新活動)

//DrawerActivity code 
@Override 
public void OnEventItemClick(ZEvent zEventObject) { 

    Log.i("URI uri", ""+zEventObject.getEventName()); 

    zEventItem = (ZEvent)zEventObject; 
    Bundle b = new Bundle(); 
    b.putParcelable("EVENT_ITEM", zEventItem); 

    Intent i = new Intent(DrawerActivity.this, EventDisplayActivity.class); 
    i.putExtra("DUMMY","dummytext"); 
    i.putExtras(b); 
    startActivity(i); 

} 

// EventDisplayActivity 

@Override 
protected void onInit(ModelBase... data) { 

    System.out.println("onInit() Called in Event Display Activity"); 

    SharedPreferences sessionkey = getApplicationContext().getSharedPreferences("session_detail", Context.MODE_PRIVATE);   
    session_token = sessionkey.getString("session", ""); 
    logged_user_type = sessionkey.getString("user_type", ""); 
    logged_user_id = sessionkey.getString("user_id", ""); 

    Intent i = getIntent(); 
    Bundle extras = i.getExtras(); 
    ZEvent zEventbundle = extras.getParcelable("EVENT_ITEM"); 


    mProposalId = zEventbundle.getProposalID(); 
    String mDummy = getIntent().getStringExtra("DUMMY");  

    Log.i("id>>>",""+mProposalId +"Dummy"+mDummy); 

    params = new RequestParams(); 
    params.put("token", session_token); 
    params.put("proposal_id", mProposalId); 

mApiClient.getView(eventViewURL, params, eventResponse); 

}  

//LOgCat 

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{gg.zing/gg.zin.zing.events.activities.EventDisplayActivity}: java.lang.NullPointerException 

Caused by: java.lang.NullPointerException 
at gg.zin.zing.events.activities.EventDisplayActivity.onInit(EventDisplayActivity.java:431) 

回答

0

我假設你ZEvent實現Parcelable ... 如果是的話,然後把在使用putExtra意圖(而不是創建新的軟件包,並使用putExtras),並提取使用getParcelableExtra以同樣的方式。

// Put  
final Intent intent = new Intent(context, <class>); 
intent.putExtra("MY_EVENT", obj); // <-- Should be parcelable 
// Get. 
ZEvent obj = intent.getParcelableExtra("MY_EVENT"); 
+0

肯定。我沒有將ZEvent(Model class)實現爲Parcelable。您建議發送數據對象的方式也是正確的。感謝您的幫助 – cham

-1

您還可以定義在用於保存在靜態字段中的數據類的靜態屬性。

public class AppData{ 
    public static String user = "Ben"; 
} 

您可以使用它像這樣:

public void setUser(String user){ 
    AppData.user = user; 
} 
+0

您必須發送帶有捆綁或事件(接口)的參數,如果您使用靜態數據,您將獲得超出範圍的內存 –

相關問題