将整数从片段传递到片段 - sectionsPageAdapter,ViewPager

时间:2013-08-31 16:54:19

标签: android android-fragments integer android-viewpager fragment

我想做什么:

我有一个托管两个片段的活动 - 托管Fragment1.java和Fragment2.java的CreateActivity.java。

当在Fragment1中单击ListFragment中的项目时,我想切换到查看Fragment2并根据Fragment1列表中单击的位置显示一些信息。

我试图像这样传递列表的“位置”值:

用户单击Fragment1中的ListFragment项,然后此代码将“position”的值发送到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中,调用以下方法来获取整数“position”:

    @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吗?

由于

1 个答案:

答案 0 :(得分:2)

您似乎在getArguments()中呼叫onCreateView。到那时,还没有任何论据可以获得。

覆盖Fragment2中的setArguments并将相关代码显示在第一个Fragment中。

这必须进入你的Fragment2:

 @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        int Position = args.getInt(ARG_POSITION, position);

        // now adjust what ever you want to display
    }