Android Studio - 从片段类打开导航抽屉

时间:2016-12-30 16:49:13

标签: android android-layout android-fragments android-drawer

我正在尝试通过单击按钮打开并填充带有单独片段的导航抽屉。抽屉本身只是在activity_main.xml中声明的空RelativeLayouts。

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>

main_view会在MainActivity中填充一个片段,所以它看起来像这样: enter image description here

MainActivity类:

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类:

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();
        }
    });
}

从MainActivity类完成时,同样的原则也适用。但是当我尝试从fragment类中打开抽屉时,会抛出此空指针错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)'

1 个答案:

答案 0 :(得分:0)

尝试检查getView()的返回值。

我认为frag1的根视图位于R.layout.frag1,并且它没有drawer_layoutdrawer_left