Daniel Kim
Daniel Kim

Reputation: 267

Android: Switching between different fragments using tabs

I'm trying to implement toolbar tabs, and have each tab inflate a different fragment, however I am stuck with the error:

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

Here is my Adapter in 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];
    }
}

and 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;
}

any help would be appreciated, thanks!

edit:

Here is the full stack trace. I changed the Adapter to use return RecordFragment.newInstance(position) instead, but still seeing the same errors.

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)

Upvotes: 2

Views: 2015

Answers (2)

Kostas Drak
Kostas Drak

Reputation: 3260

You can use

onCreateView() method in each fragment and by using

ViewHolders

and

LayoutInflater you can generate any view that you would like...

Upvotes: 0

matiash
matiash

Reputation: 55380

getArguments() is returning null (hence, the NPE at getArguments().getInt(ARG_POSITION)) because you haven't called setArguments() after creating the Fragment.

The usual pattern is to have a factory method in the Fragment class that preloads the arguments. In this case, it should be something more or less like:

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

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

    return f;
}

Then create the Fragment using this method, instead of calling the constructor directly.

Upvotes: 2

Related Questions