在DrawerLayout使用NavigationView后程序闪退,求帮助;

qq_37546625 2018-07-21 07:51:43
在DrawerLayout使用NavigationView后程序闪退,但是用TextView就正常了

public class DesignActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;

private NavigationView mNavView;

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_design); //在这里报错
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
mNavView = findViewById(R.id.nav_view);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.od_menu);
}
mNavView.setCheckedItem(R.id.nav_call);
mNavView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
mDrawerLayout.closeDrawers();
return true;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.backup:
ToastUtil.showMsg(this, "You clicked Backup");
break;
case R.id.delete:
ToastUtil.showMsg(this, "You clicked Delete");
break;
case R.id.settings:
ToastUtil.showMsg(this, "You clicked Setting");
break;
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
break;

default:
break;
}
return true;
}
}


页面布局文件
<?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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/AlertDialog.AppCompat.Light"/>

</FrameLayout>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:menu="@menu/nav_menu"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"/>


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


如果把NavigetionView中的layout_gravity="start"去掉后可以看到

menu的xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_call"
android:icon="@drawable/call"
android:title="Call"/>

<item
android:id="@+id/nav_friends"
android:icon="@drawable/firend"
android:title="Friends"/>

<item
android:id="@+id/nav_location"
android:icon="@drawable/location"
android:title="Location"/>

<item
android:id="@+id/nav_mail"
android:icon="@drawable/mail"
android:title="Mail"/>

<item
android:id="@+id/nav_task"
android:icon="@drawable/task"
android:title="Tasks"/>
</group>
</menu>


nav_header的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:padding="10dp"
android:background="?attr/colorPrimary">

<android.support.v4.widget.CircleImageView
android:id="@+id/icon_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/panda"
android:layout_centerInParent="true"/>

<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/mail"
android:text="Tony Green"
android:textColor="#FFF"
android:textSize="14sp"/>

<TextView
android:id="@+id/mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="tonygreendev@mail.com"
android:textColor="#FFF"
android:textSize="14sp"/>

</RelativeLayout>


最后Log奉上求大神相助
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.helloworld, PID: 10420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.materialdesign.DesignActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2488)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548)
at android.app.ActivityThread.access$1100(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5613)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.helloworld.materialdesign.DesignActivity.onCreate(DesignActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6362)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548) 
at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5613) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

...全文
279 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_37546625 2018-07-21
  • 打赏
  • 举报
回复
在Toolbar的布局文件中
qq_37546625 2018-07-21
  • 打赏
  • 举报
回复
自检成功了!
是布局文件中这句
app:popupTheme="@style/AlertDialog.AppCompat.Light"
修改为:
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
就好了!!!

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧