我有一个带有3个滑动标签的应用。所以我首先创建了mainActivity然后创建了3个片段。
我正在尝试在tabbedactivity中实现一个导航器,但是当我尝试检索DrawerLayout时,我收到错误“NullPointerException”。
我正在做主要活动的所有代码,就像我应该做的那样,不是吗?或者我需要在每个片段中创建导航抽屉?
这是我的mainactivity xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.dasilvadd.students.OngletCours">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/menu_navigation"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_gravity="start"
android:visibility="invisible"
app:menu="@menu/navigation_menu"></android.support.design.widget.NavigationView>
在我的mainActivity中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onglet_cours);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//récuperation des differents types de variables pour le menu de navigation
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToogle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open,R.string.close);
//j'ajoute a mon DrawerLayout et je synchronise
mDrawerLayout.addDrawerListener(mToogle);
mToogle.syncState();
//je récupère le menu de navigation
NavigationView navigation = (NavigationView)findViewById(R.id.menu_navigation);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
setTitle("Mes cours");
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
//ce Listener permet de detecter quel item a été choisi dans mon menu
navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId())
{ //si c'est "Mes cours" qui a été choisi
case R.id.mes_cours:
//redirection vers la page "mes cours"
Intent cours = new Intent(OngletCours.this,MesCours.class);
startActivity(cours);
return true;
//actions a faire si "mes branches" a été choisi
case R.id.mes_branches:
Intent branche = new Intent(OngletCours.this,MesBranches.class);
branche.putExtra("ajouter","cours");
startActivity(branche);
return true;
case R.id.mes_jalons:
Intent jalons = new Intent(OngletCours.this,PageiTude.class);
startActivity(jalons);
}
return true;
}
});
@update - 记录错误
Caused by: java.lang.NullPointerException
at com.example.dasilvadd.students.OngletCours.onCreate(OngletCours.java:61)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
你能解释一下这个错误吗?我希望这不是一个愚蠢的错误...谢谢你!
答案 0 :(得分:0)
1。您正试图引用错误的DrawerLayout
ID drawerLayout2
2。添加ActionBarDrawerToggle
听众。
试试这个:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onglet_cours);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToogle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
..............
.....................
}