0

I'm trying to open and populate a navigation drawer with a separate fragment by clicking on a button. The drawers themselves are only empty RelativeLayouts declared in activity_main.xml.

activity_main.xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--Main View-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="xxx.MainActivity">
</RelativeLayout>

<!--Left drawer-->
<RelativeLayout android:id="@+id/drawer_left"
    android:layout_width="290dp"
    android:layout_height="match_parent"
    android:layout_gravity="start">
</RelativeLayout>

<!--Right drawer-->
<RelativeLayout android:id="@+id/drawer_right"
    android:layout_width="290dp"
    android:layout_height="match_parent"
    android:layout_gravity="end">
</RelativeLayout>

The main_view gets populated with a fragment in MainActivity, so it will look like this: enter image description here

MainActivity class:

public class MainActivity extends AppCompatActivity {

android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fragmentManager = getFragmentManager();

    //Set fragment
    Frag1 frag1 = new Frag1();
    fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container,frag1).commit();
}

Frag1 class:

public class Frag1 extends Fragment {
ImageButton imageButtonOpenDrawer;

DrawerLayout mDrawerLayout;
RelativeLayout mDrawer_left;

android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.frag1, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    View v = getView();
    imageButtonOpenDrawer = (ImageButton) v.findViewById(R.id.imageButtonOpenDrawer);

    fragmentManager = getFragmentManager();

    mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
    mDrawer_left = (RelativeLayout) v.findViewById(R.id.drawer_left);

    imageButtonOpenDrawer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DrawerFragment drawerFragment = new DrawerFragment();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container,drawerFragment).commit();
        }
    });
}

The same principle works when done from MainActivity class. But when I try to open the drawer from the fragments class, this null pointer error gets thrown:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference

1 Answers1

0

Try to check returns of getView().

I think that frag1's root view is in R.layout.frag1 and it doesn't have drawer_layout and drawer_left.

Choim
  • 372
  • 1
  • 10
  • But R.layout.frag1 is placed inside the drawer layout. So it should find it. –  Dec 31 '16 at 11:52
  • `getView()` returns view inflated by using `R.layout.frag1`. It dosen't have its parent(`drawer_layout`). – Choim Jan 02 '17 at 01:40
  • 1
    It dosen't have its parent(`drawer_layout`) because `attachToRoot` is false when you inflate view in `onCreateView`. if `attachToRoot` is true, `getView()` returns `container(fragment_container)` . – Choim Jan 02 '17 at 01:44
  • I understand. I tried to set the attachToRoot parameter to true. But now i get another error: `java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first` –  Jan 04 '17 at 13:47
  • sorry, you should have passed `attachToRoot=false` in fragment's `onCreateView()` because The view would be added to its parent when fragment added. – Choim Jan 06 '17 at 05:18