gpt4 book ai didi

java - 如何修复 "Error inflating class android.support.design.widget.NavigationView"

转载 作者:行者123 更新时间:2023-11-29 09:26:49 25 4
gpt4 key购买 nike

我想创建一个应用程序,其中 NavigationView 和 Bottom Navigation 相互配合,但在输出中我得到了“Run”中的下一个字符串

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mederov.timelord, PID: 24070 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mederov.timelord/com.mederov.timelord.MainActivity}: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2785) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: android.view.InflateException: Binary XML file line #38: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.NavigationView Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.view.LayoutInflater.createView(LayoutInflater.java:647) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.mederov.timelord.MainActivity.onCreate(MainActivity.java:24) at android.app.Activity.performCreate(Activity.java:7033) at android.app.Activity.performCreate(Activity.java:7024) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2738) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.RuntimeException: Unexpected end of document at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:215) at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:128) at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:264) at android.support.design.widget.NavigationView.(NavigationView.java:189) at android.support.design.widget.NavigationView.(NavigationView.java:104) at java.lang.reflect.Constructor.newInstance0(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:334)  at android.view.LayoutInflater.createView(LayoutInflater.java:647)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at com.mederov.timelord.MainActivity.onCreate(MainActivity.java:24)  at android.app.Activity.performCreate(Activity.java:7033)  at android.app.Activity.performCreate(Activity.java:7024)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2738)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1596)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

.

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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_container"
android:layout_above="@id/nav_bar"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/nav_bar"
app:menu="@menu/bottom_navigation"
android:background="@color/colorWhite"/>

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

</RelativeLayout>


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

Java Class

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.nav_bar);
bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
drawer,
toolbar,
R.string.nav_open_drawer,
R.string.nav_close_drawer);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
Fragment fragment = new AimsFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_frame, fragment);
ft.commit();;
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_veri:
selectedFragment = new AimsFragment();
break;
case R.id.nav_diary:
selectedFragment = new DiaryFragment();
break;
case R.id.nav_appoint:
selectedFragment = new CalendarFragment();
break;
}

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
return true;
}

};

public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
Intent intent = null;
switch(id){
case R.id.nav_aims:
fragment = new AimsFragment();
break;
case R.id.nav_reminder:
fragment = new ReminderFragment();
break;
case R.id.nav_statics:
fragment = new StaticsFragment();
break;
case R.id.nav_schedule:
fragment = new ScheduleFragment();
break;
case R.id.nav_project:
fragment = new ProjectFragment();
break;
case R.id.nav_settings:
fragment = new SettingsFragment();
break;
case R.id.nav_info:
fragment = new InfoFragment();
break;
default:
fragment = new AimsFragment();
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
} else {
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

}

Gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mederov.timelord"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
buildToolsVersion '28.0.3'
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
}

Menu

<?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_aims"
android:icon="@drawable/idea_aims"
android:title="@string/nav_aims"
android:checked="true" />
<item
android:id="@+id/nav_reminder"
android:icon="@drawable/reminder"
android:title="@string/nav_reminder" />
<item
android:id="@+id/nav_statics"
android:icon="@drawable/statics"
android:title="@string/nav_statics" />
<item
android:id="@+id/nav_schedule"
android:icon="@drawable/scheldule"
android:title="@string/nav_schedule" />
<item
android:id="@+id/nav_project"
android:icon="@drawable/project"
android:title="@string/nav_project" />
</group>

<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/settings"
android:title="@string/nav_settings" />
<item
android:id="@+id/nav_info"
android:icon="@drawable/info"
android:title="@string/nav_info"/>

</menu>

</menu>

Help me please I can't realize how fix this error)

最佳答案

我有同样的错误,我修正了它。问题出在 vector 图标中,如果您尝试在 api 19 上运行它, vector 只能在 api 级别 21+ 上运行 - 他的意愿不适用于您。

关于java - 如何修复 "Error inflating class android.support.design.widget.NavigationView",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54636695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com