我所試圖做的事:傳遞整數片段片段 - sectionsPageAdapter,ViewPager
我有承載兩個片段的活動 - CreateActivity.java承載Fragment1.java和Fragment2.java。
當在Fragment1中點擊ListFragment中的項目時,我想切換到查看Fragment2並根據Fragment1中列表中單擊的位置顯示一些信息。
我試圖通過列表中的「位置」的價值,像這樣:在片段1 ListFragment項目
用戶點擊這個代碼,然後將「位置」向CreateActivity.java值:
OnMemeSelectedListener mCallback;
public interface OnListSelectedListener {
public void onArticleSelected(int position);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnListSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallback.onArticleSelected(position);
}
在CreateActivity.java下面的方法被調用,以獲得整數「位置」:
@Override
public void onArticleSelected(int position) {
// Fragment2 newFragment = new Fragment2();
Bundle args = new Bundle();
args.putInt(Fragment2.ARG_POSITION, position);
Fragment2 fragobj = new Fragment2();
fragobj.setArguments(args);
Log.w("myApp", "" + position);
//Switch to Fragment2 Tab
mViewPager.setCurrentItem(1);
}
截至這是完全的工作作爲日誌輸出正確的位置列表。現在獲得Fragment2.class我寫了下面這行的整數:
int Position = getArguments().getInt(ARG_POSITION, position);
但是我在開始CreateActivity.java得到NullPointError在我的日誌:
08-31 17:45:04.102: E/AndroidRuntime(14779): FATAL EXCEPTION: main
08-31 17:45:04.102: E/AndroidRuntime(14779): java.lang.NullPointerException
08-31 17:45:04.102: E/AndroidRuntime(14779): at com.meme.hdmeme.Fragment2.onCreateView(Fragment2.java:59)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.View.measure(View.java:15518)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.View.measure(View.java:15518)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.View.measure(View.java:15518)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-31 17:45:04.102: E/AndroidRuntime(14779): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2313)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.View.measure(View.java:15518)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.os.Handler.handleCallback(Handler.java:725)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.os.Looper.loop(Looper.java:137)
08-31 17:45:04.102: E/AndroidRuntime(14779): at android.app.ActivityThread.main(ActivityThread.java:5195)
08-31 17:45:04.102: E/AndroidRuntime(14779): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 17:45:04.102: E/AndroidRuntime(14779): at java.lang.reflect.Method.invoke(Method.java:511)
08-31 17:45:04.102: E/AndroidRuntime(14779): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-31 17:45:04.102: E/AndroidRuntime(14779): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-31 17:45:04.102: E/AndroidRuntime(14779): at dalvik.system.NativeStart.main(Native Method)
我一直停留在這幾個小時和無法找到一個解決方法,在所有59行(導致NPE)是:
int Position = getArguments().getInt(ARG_POSITION, position);
我必須創建之前就設置Fragment2的論點?我想在這個活動中創建這個數據之後傳遞給Fragment2,有什麼想法嗎?
感謝
通過這個你的意思是創建片段2(onCreateview外)調用setArguments()的方法?道歉我仍然真的在編碼 –
新作出了一些編輯.. – pumpkee
解決方案的作品,非常感謝你:) –