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.