我開始之前,我有過的問題看,如之間的物體:安卓:傳遞片段
Passing data between fragments: screen overlap How to pass values between Fragments
以及Android的文檔:
http://developer.android.com/training/basics/fragments/communicating.html
以及這篇文章:
http://manishkpr.webheavens.com/android-passing-data-between-fragments/
雖然上面提到的所有情況都與我的相似,但並不完全相同。我跟着一個很好的教程在這裏(我的代碼有些部分是基於這篇文章):
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
我有以下文件:
RegisterActivity.java
NonSwipeableViewPager.java
ScreenSliderAdapter .java
RegisterOneFragment.java
RegisterTwoFragment.java
而以下佈局:
activity_register.xml
fragment_register_one.xml
fragment_register_two.xml
什麼,我想實現的是傳遞一個序列化對象從RegisterFragmentOne到RegisterFragmentTwo。
到目前爲止,這是我做了什麼(一些代碼省略):
RegisterActivity.java
public class RegisterActivity extends FragmentActivity
implements RegisterOneFragment.OnEmailRegisteredListener{
public static NonSwipeableViewPager viewPager;
private ScreenSliderAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Initilization
mAdapter = new ScreenSliderAdapter(getSupportFragmentManager());
viewPager = (NonSwipeableViewPager) findViewById(R.id.pager);
viewPager.setAdapter(mAdapter);
}
public void onEmailRegistered(int position, Registration regData){
Bundle args = new Bundle();
args.putSerializable("regData", regData);
viewPager.setCurrentItem(position, true);
}
}
ScreenSliderAdapter.java
public class ScreenSliderAdapter extends FragmentPagerAdapter{
public ScreenSliderAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new RegisterOneFragment();
case 1:
return new RegisterTwoFragment();
case 2:
return new RegisterThreeFragment();
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
NonSwipeableViewPager。 java(擴展ViewPager類,並覆蓋以下)
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
RegisterOneFragment.java
public class RegisterOneFragment extends Fragment {
OnEmailRegisteredListener mCallBack;
public interface OnEmailRegisteredListener {
/** Called by RegisterOneFragment when an email is registered */
public void onEmailRegistered(int position, Registration regData);
}
public void onAttach(Activity activity){
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallBack = (OnEmailRegisteredListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnEmailRegisteredListener");
}
}
... And some to execute some HTTP request via separate thread...
}
我試圖做到的是,當永遠的用戶按下上RegisterOneFragment按鈕,數據將被髮送到服務器(並通過JSON返回一些驗證)。如果返回的數據有效,應用程序應該轉到下一個RegistrationTwoFragment片段。
我對如何在片段之間傳遞對象感到困惑,因爲我的片段是使用Adapter創建的。然後,該適配器被附加到我的活動。
任何人都可以幫助我嗎? THX
編輯1:
我試圖建立一個快捷方式(可惜不工作),像這樣:
在RegisterActivity我創建:
public Registration regData;
和RegisterOneFragment:
/* PLACED ON POST EXECUTE */
((RegisterActivity)getActivity()).regData = regData;
Finall Ÿ稱它在RegisterTwoFragment
Registration regData;
regData = ((RegisterActivity) getActivity()).regData;
它拋出一個NullPointerException異常
編輯2
只是要清楚,RegisterActivty包含多個片段。用戶可以在片段之間導航的唯一方法是單擊按鈕。該活動沒有選項卡欄。
你可以告訴http://stackoverflow.com/questions/40126867/how-to-access-custom-arraylist-in-fragment – albert