Android: Switching between different fragments using tabs

Daniel Kim

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)
matiash

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Switching between Android Navigation Drawer image and Up caret when using fragments

From Dev

Switching between Fragments with button

From Dev

My Navigation bar is not switching between fragments in Android Studio Kotlin

From Dev

Android 3 tabs/fragments and with different action item for each

From Dev

Switching between fragments with back button

From Dev

Does Notepad++ support switching between tabs using numbers?

From Dev

Switching between different workbooks without using .activate

From Dev

ClassCastException while using fragments in android actionbar tabs and tab listener

From Dev

Switching between tabs in modal in angular

From Dev

Switching between tabs in modal in angular

From Dev

Android: Replacing fragments inside tabs

From Dev

Using Android L animations for switching between activities

From Dev

Add Image to Tabs using Fragments

From Dev

Switching between different platforms

From Dev

Will Activity onpause be called when switching between fragments

From Dev

NullPointerException when switching between fragments containing RecyclerView

From Dev

RecyclerView items disappear after switching between fragments

From Dev

Switching between fragments does not call onStop method

From Dev

How to handle camera while switching between fragments?

From Dev

Android - FAB to hide when navigating between different fragments in a viewpager

From Dev

How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

From Dev

Using viewer page to switch between two fragments - however it disappears after switching into a fragment

From Dev

How to remove the gap between tabs and fragments

From Dev

Communication between fragments in android

From Dev

Android communication between fragments

From Dev

Switching Fragments in navigation drawer using onNavigationItemSelected()

From Dev

switching between tabs in sublime "ctrl+tab"

From Dev

Shortcut for switching between tabs in Xcode now?

From Dev

Visual Studio 2013 Slow Switching between Tabs

Related Related

  1. 1

    Switching between Android Navigation Drawer image and Up caret when using fragments

  2. 2

    Switching between Fragments with button

  3. 3

    My Navigation bar is not switching between fragments in Android Studio Kotlin

  4. 4

    Android 3 tabs/fragments and with different action item for each

  5. 5

    Switching between fragments with back button

  6. 6

    Does Notepad++ support switching between tabs using numbers?

  7. 7

    Switching between different workbooks without using .activate

  8. 8

    ClassCastException while using fragments in android actionbar tabs and tab listener

  9. 9

    Switching between tabs in modal in angular

  10. 10

    Switching between tabs in modal in angular

  11. 11

    Android: Replacing fragments inside tabs

  12. 12

    Using Android L animations for switching between activities

  13. 13

    Add Image to Tabs using Fragments

  14. 14

    Switching between different platforms

  15. 15

    Will Activity onpause be called when switching between fragments

  16. 16

    NullPointerException when switching between fragments containing RecyclerView

  17. 17

    RecyclerView items disappear after switching between fragments

  18. 18

    Switching between fragments does not call onStop method

  19. 19

    How to handle camera while switching between fragments?

  20. 20

    Android - FAB to hide when navigating between different fragments in a viewpager

  21. 21

    How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

  22. 22

    Using viewer page to switch between two fragments - however it disappears after switching into a fragment

  23. 23

    How to remove the gap between tabs and fragments

  24. 24

    Communication between fragments in android

  25. 25

    Android communication between fragments

  26. 26

    Switching Fragments in navigation drawer using onNavigationItemSelected()

  27. 27

    switching between tabs in sublime "ctrl+tab"

  28. 28

    Shortcut for switching between tabs in Xcode now?

  29. 29

    Visual Studio 2013 Slow Switching between Tabs

HotTag

Archive