创建导航抽屉时强制关闭

时间:2017-08-08 06:25:10

标签: android xml android-fragments navigation-drawer

我有导航抽屉的代码,但不起作用 当我运行app force关闭时 对不起,如果我不能写英文很好

主要活动
此活动中有抽屉布局代码

public class List extends AppCompatActivity {
DrawerLayout dl;
ListView list;
String[] item={"test","test2","test3","test4","test5","test6 "};
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ListView)findViewById(R.id.list1);
    dl=(DrawerLayout)findViewById(R.id.dl);
    toolbar=(Toolbar) findViewById(R.id.app_bar_layout);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    DraweLayoutFragment dl = (DraweLayoutFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    dl.setup((DrawerLayout)findViewById(R.id.drawerlayout),toolbar);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.txt,item);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position==0){
                Intent intent = new Intent(List.this,biographi.class);
                startActivity(intent);
            }else if(position==1){
               Intent intent1 = new Intent(List.this,Rooidad.class);
                startActivity(intent1);
            }else if(position==2){
                Intent intent1 = new Intent(List.this,Asar.class);
                startActivity(intent1);
            }
        }
    });
}
}

这是主要活动的xml 这个xml中有一个用于导航抽屉的片段

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mojabi"
    android:layout_gravity="center">

    <include layout="@layout/app_bar"
        android:id="@+id/app_bar"/>

    <ListView
        android:id="@+id/list1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:dividerHeight="10dp" />
</RelativeLayout>
<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="javadmojabi.ir.DraweLayoutFragment"
    app:layout="@layout/fragment_drawe_layout"
    tools:layout="@layout/fragment_drawe_layout" />

</android.support.v4.widget.DrawerLayout>

这个鳕鱼用于主xml中的片段

public class DraweLayoutFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
private DrawerLayout my_drawer_layout;
private ActionBarDrawerToggle drawer_Toggle;

public DraweLayoutFragment() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);
}

public void setup(DrawerLayout dl, Toolbar tl) {
    my_drawer_layout = dl;
    drawer_Toggle = new ActionBarDrawerToggle(getActivity(), dl, tl, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };
    my_drawer_layout.addDrawerListener(drawer_Toggle);
  }
}

片段xml的这段代码

<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=".DraweLayoutFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

logcat:

08-13 18:23:36.803 23245-23245/javadmojabi.ir E/AndroidRuntime: FATAL 
EXCEPTION: main
                                                            Process: 
javadmojabi.ir, PID: 23245

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{javadmojabi.ir/javadmojabi.ir.List}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null 
object reference
                                                                at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2530)
                                                                at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2610)
                                                                at 
android.app.ActivityThread.access$700(ActivityThread.java:181)
                                                                at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1476)
                                                                at 
android.os.Handler.dispatchMessage(Handler.java:111)
                                                                at 
android.os.Looper.loop(Looper.java:194)
                                                                at 
android.app.ActivityThread.main(ActivityThread.java:5660)
                                                                at 
java.lang.reflect.Method.invoke(Native Method)
                                                                at 
java.lang.reflect.Method.invoke(Method.java:372)
                                                                at 

 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:963)
                                                                at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
                                                             Caused by: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a 
null 
object reference
                                                                at 
javadmojabi.ir.List.onCreate(List.java:30)
                                                                at 
android.app.Activity.performCreate(Activity.java:6156)
                                                                at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2477)
                                                                at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2610) 
                                                                at 
android.app.ActivityThread.access$700(ActivityThread.java:181) 
                                                                at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1476) 
                                                                at 
android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                at 
android.os.Looper.loop(Looper.java:194) 
                                                                at 
android.app.ActivityThread.main(ActivityThread.java:5660) 
                                                                at 
java.lang.reflect.Method.invoke(Native Method) 
                                                                at 
   java.lang.reflect.Method.invoke(Method.java:372) 
                                                                at  

 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:963) 
                                                                   at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758) 
08-13 18:23:36.856 713-906/? E/WifiStateMachine:  ConnectedState (when=-1ms 
what=147461!SCAN_RESULTS_EVENT 0 0 found=2 known=1 got=2 bcn=۰ con=۱
08-13 18:23:36.856 713-906/? E/WifiStateMachine:  L2ConnectedState (when=-2ms 
what=147461!SCAN_RESULTS_EVENT 0 0 found=2 known=1 got=2 bcn=۰ con=۱

1 个答案:

答案 0 :(得分:0)

你的onCreateView应该返回片段xml

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View view = (FrameLayout) 
      inflater.inflate(R.layout.fragment, container, 
      false);

    return view;
}