Android:使用标签在不同的片段之间切换

时间:2014-12-25 20:31:19

标签: android tabs fragment toolbar

我正在尝试实现工具栏标签,并且每个标签都会膨胀一个不同的片段,但是我遇到了错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danielkim.soundrecorder/com.danielkim.soundrecorder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference

以下是MainActivity.java中的适配器:

public class MyAdapter extends FragmentPagerAdapter {
    private String[] titles = { "Record", "Saved Recordings" };
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position){
            case 0:{
                return RecordFragment.newInstance(position);
            }
            case 1:{
                return FileViewerFragment.newInstance(position);
            }
        }
        return null;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

RecordFragment.java

public static RecordFragment newInstance(int position) {
    RecordFragment f = new RecordFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    position = getArguments().getInt(ARG_POSITION);
}

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View recordView = inflater.inflate(R.layout.fragment_record, container, false);

    mChronometer = (Chronometer) recordView.findViewById(R.id.chronometer);

    mRecordButton = (FloatingActionButton) recordView.findViewById(R.id.btnRecord);
    mRecordButton.setColorNormal(getResources().getColor(R.color.accent));
    mRecordButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onRecord(mStartRecording);
            mStartRecording = !mStartRecording;
        }
    });

    mPauseButton = (ImageButton) recordView.findViewById(R.id.btnPause);
    mPauseButton.setVisibility(View.GONE); //hide pause button before recording starts

    return recordView;
}

任何帮助将不胜感激,谢谢!

编辑:

这是完整的堆栈跟踪。我将适配器更改为使用return RecordFragment.newInstance(position),但仍然看到相同的错误。

12-25 16:16:42.845    2889-2889/com.danielkim.soundrecorder E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.danielkim.soundrecorder, PID: 2889
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danielkim.soundrecorder/com.danielkim.soundrecorder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
            at com.danielkim.soundrecorder.fragments.RecordFragment.onCreate(RecordFragment.java:76)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
            at android.app.Activity.performStart(Activity.java:5949)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

2 个答案:

答案 0 :(得分:2)

getArguments()返回null(因此,getArguments().getInt(ARG_POSITION)处的NPE)因为您在创建片段后没有调用setArguments()

通常的模式是在Fragment类中有一个预加载参数的工厂方法。在这种情况下,它应该或多或少像:

public static RecordFragment newInstance(int position) {
    RecordFragment f = new RecordFragment();

    Bundle args = new Bundle();
    args.putInt(ARG_POSITION, position);
    f.setArguments(args);

    return f;
}

然后使用此方法创建Fragment,而不是直接调用构造函数。

答案 1 :(得分:0)

您可以使用

每个片段中的

onCreateView()方法并使用

ViewHolders

LayoutInflater您可以生成任何您想要的视图...