2

I have a ActivityFragments with threeButtons and FragmentOne, FragmentTwo, FragmentThree classes. When Button1 is pressed it has to show FragmentOne and hide FragmentTwo and FragmentThree. If Button2 is pressed it has to show FragmentTwo and hide FragmentOne and FragmentThree. Similarly ifButton3 is pressed it has to show FragmentThree and hide FragmentTwo and FragmentOne. My code didn't work showing NullPointerException E/AndroidRuntime(362): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)

Logcat:

04-08 08:10:31.830: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-08 08:10:31.830: E/AndroidRuntime(362): java.lang.NullPointerException
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:656)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Handler.handleCallback(Handler.java:587)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.os.Looper.loop(Looper.java:123)
04-08 08:10:31.830: E/AndroidRuntime(362):      at android.app.ActivityThread.main(ActivityThread.java:4627)
04-08 08:10:31.830: E/AndroidRuntime(362):      at java.lang.reflect.Method.invokeNative(Native Method)
04-08 08:10:31.830: E/AndroidRuntime(362):      at java.lang.reflect.Method.invoke(Method.java:521)
04-08 08:10:31.830: E/AndroidRuntime(362):      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-08 08:10:31.830: E/AndroidRuntime(362):      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-08 08:10:31.830: E/AndroidRuntime(362):      at dalvik.system.NativeStart.main(Native Method)

You can find it here too

And this is the code : MainActivity.java

 public class MainActivity extends FragmentActivity implements OnClickListener {
    Button b1, b2, b3;
    Fragment f1, f2, f3;
    FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        manager = getSupportFragmentManager();

        f1 = manager.findFragmentById(R.id.first);
        f2 = manager.findFragmentById(R.id.second);
        f2 = manager.findFragmentById(R.id.third);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            if(b1.isPressed()){
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.hide(f2);
            transaction.hide(f3);
            transaction.show(f1);
            transaction.commit();
            }
            break;
        case R.id.button2:
            if(b2.isPressed()){
            FragmentTransaction transaction1 = getSupportFragmentManager()
                    .beginTransaction();
            transaction1.hide(f1);
            transaction1.hide(f3);
            transaction1.show(f2);
            transaction1.commit();
            }
            break;
        case R.id.button3:
            if(b3.isPressed()){
            FragmentTransaction transaction2 = getSupportFragmentManager()
                    .beginTransaction();
            transaction2.hide(f1);
            transaction2.hide(f2);
            transaction2.show(f3);
            transaction2.commit();
            }
            break;
        }

    }

    }


    public class FirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first, container, false);
        return view;
    }
    }
    public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.second, container, false);
        return view;
    }
    }
    public class Third extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.third, container, false);
        return view;
    }
    }

Layout file : main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:text="Button 2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="Button 3" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.FirstFragment" />

    <fragment
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.SecondFragment" />

    <fragment
        android:id="@+id/third"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.fragmentworks.Third" />
</LinearLayout>

2

1 Answer 1

2

You have a typo:

f1 = manager.findFragmentById(R.id.first);
f2 = manager.findFragmentById(R.id.second);
f2 = manager.findFragmentById(R.id.third);

last line should be f3 =....

Ali.

2
  • oh my bad!! Thanks for noticing it :)
    – User1001
    Apr 8, 2013 at 5:34
  • You can then accept that answer.
    – Ali Naddaf
    Apr 8, 2013 at 12:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.