0

I am using multiple fragments in my main activity now I tried to access childs of each fragment but it giving me null exception when its childs are access.

On clicking button1 not changing data and on clicking button2 app crashing. Here is my code:

MainActivity

public class MainActivity extends Activity {


    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    Fragment fr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
    }

    public void selectFrag(View view) {
        Fragment fr;

        if(view == findViewById(R.id.button1)) {
            //add a fragment 
            fr = new FragmentOne();
            TextView textView1 = (TextView) fr.getView().findViewById(R.id.textView1);
            textView1.setText("Fragment One");

        }else {
            //add a fragment 
            fr = new FragmentTwo();
            TextView textView2 = (TextView) fr.getView().findViewById(R.id.textView2);
            textView2.setText("Fragment Two");
        }
        fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();


    }

}

FragmentOne

public class FragmentOne extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

       //Inflate the layout for this fragment

      return inflater.inflate(
              R.layout.fragment_one, container, false);
   }
}

FragmentTwo

public class FragmentTwo extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
      /**
       * Inflate the layout for this fragment
       */
      return inflater.inflate(
      R.layout.fragment_two, container, false);
   }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" /> 

   <fragment
        android:name="com.javacodegeeks.android.fragmentstest.FragmentOne"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

fragment_one.xml

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#00ffff">

       <TextView
           android:id="@+id/textView1"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="This is fragment No.1"
           android:textStyle="bold" />

</LinearLayout>

fragment_two.xml

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:orientation="vertical"
   android:background="#ffff00">

       <TextView
           android:id="@+id/textView2"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:text="This is fragment No.2"
           android:textStyle="bold" />

</LinearLayout>

CrashReport on clicking button2:

07-07 14:41:40.505: E/AndroidRuntime(15175): FATAL EXCEPTION: main
07-07 14:41:40.505: E/AndroidRuntime(15175): Process: com.javacodegeeks.android.fragmentstest, PID: 15175
07-07 14:41:40.505: E/AndroidRuntime(15175): java.lang.IllegalStateException: Could not execute method of the activity
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.view.View$1.onClick(View.java:3851)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.view.View.performClick(View.java:4466)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.view.View$PerformClick.run(View.java:18833)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.os.Handler.handleCallback(Handler.java:808)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.os.Handler.dispatchMessage(Handler.java:103)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.os.Looper.loop(Looper.java:193)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.app.ActivityThread.main(ActivityThread.java:5299)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at java.lang.reflect.Method.invoke(Method.java:515)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at dalvik.system.NativeStart.main(Native Method)
07-07 14:41:40.505: E/AndroidRuntime(15175): Caused by: java.lang.reflect.InvocationTargetException
07-07 14:41:40.505: E/AndroidRuntime(15175):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at java.lang.reflect.Method.invoke(Method.java:515)
07-07 14:41:40.505: E/AndroidRuntime(15175):    at android.view.View$1.onClick(View.java:3846)
07-07 14:41:40.505: E/AndroidRuntime(15175):    ... 11 more
07-07 14:41:40.505: E/AndroidRuntime(15175): Caused by: java.lang.NullPointerException
07-07 14:41:40.505: E/AndroidRuntime(15175):    at com.javacodegeeks.android.fragmentstest.MainActivity.selectFrag(MainActivity.java:37)
07-07 14:41:40.505: E/AndroidRuntime(15175):    ... 14 more
User42590
  • 2,473
  • 12
  • 44
  • 85

1 Answers1

0

getView() returns the view you inflate in onCreateView. As you probably know onCreateView is part of the Fragment's lifecycle, and it is up to the system when it is called, and ti happens for sure after you commit the transaction, and it can't happen before. If you have to provide values to the fragment, you can use the pair setArguments/getArguments. Use the former when you instantiate the Fragment, and the latter in onCreateView to retrieve the information and to fill up your view

Blackbelt
  • 156,034
  • 29
  • 297
  • 305