我创建了名为“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 :(得分:0)
我假设你的ZEvent实现了Parcelable ...... 如果确实如此,那么使用putExtra输入intent(而不是创建新的Bundle并使用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");
答案 1 :(得分:-1)
您还可以在类中定义静态属性,该属性用于在静态字段中保存数据。
public class AppData{
public static String user = "Ben";
}
你可以像这样使用它:
public void setUser(String user){
AppData.user = user;
}