0

I'm trying to use findViewById() to find a RelativeLayout inside a Fragment's Layout, and then add my GridView to the RelativeLayout. Here's my code:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
GridLayout gridLayout = new GridLayout(this);
relativeLayout.addView(gridLayout);

The XML file for Fragment's Layout:

<FrameLayout 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"
    tools:context="com.sepehr.dotsandlines.Game">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout">

    </RelativeLayout>
</FrameLayout>

The Error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference

Note: I'm looking for id inside MainActivity.java not the Fragment Java.

3 Answers3

0

Inside Fragment first get root view and look for id using findViewById() .Inside onCreateView method use following .

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container, 
                         Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
RelativeLayout relativeLayout = (RelativeLayout)view. findViewById(R.id.relativeLayout);
     return view;
}
Godfather
  • 833
  • 9
  • 14
0

It's really not a good idea to use findViewById in onCreateView. You might face random crashes where the view will not be found.

As per fragment lifecycle you should be doing this in onViewCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add your code here which executes after the execution of onCreateView() method.

}

For more details, you can refer Difference between onCreateView and onViewCreated in Fragment

0

I figured it out myself. First of all, I amlooking for ID in the MainActivity.java file, and Overriding onCreateView() and onViewCreated() in Game.java (The fragment java file) does not do anything! That's not the answer.

I used LayoutInflater to inflate the Fragment's Layout and use that to find the ID (objectCreate() is called by a button in the activity_main Layout)

public void objectCreate(View view){
   View v=inflater.inflate(R.layout.fragment_game, null, false);
   RelativeLayout relativeLayout = v.findViewById(R.id.relativeLayout);
   GridLayout gridLayout = new GridLayout(this);
   relativeLayout.addView(gridLayout);

   //adding some other views to the GridLayout

   //change layout to MainLayout.xml (Which contains the FrameLayout):
   v=inflater.inflate(R.layout.activity_main, null, false);
   v.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
   setContentView(v);
   //show the fragment:
   Class fragmentClass = Game.class;
   Fragment fragment = (Fragment) fragmentClass.newInstance();
   FragmentManager fragmentManager = getSupportFragmentManager();
   fragmentManager.beginTransaction().replace(R.id.fl, fragment).commit();
}

Another problem is, I now don't get an error, but the screen is still blank and none of the views are shown!