简体   繁体   English

由 android 工作室制作的导航抽屉:android.view.InflateException:错误充气 class 片段

[英]navigation drawer made from android studio : android.view.InflateException: Error inflating class fragment

I am a complete beginner in android studio with java and I have run across this error which seems to be very frequent here.我是 android 工作室和 java 的完整初学者,我遇到了这个错误,这在这里似乎很常见。 I have seen solutions but nothing has helped so far.我已经看到了解决方案,但到目前为止没有任何帮助。 In my code I have created a navigation drawer using the already made one from android studio.在我的代码中,我使用 android 工作室已经制作的导航抽屉创建了一个导航抽屉。 When I try to run the app I get:当我尝试运行应用程序时,我得到:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.covidmobileapp/com.example.covidmobileapp.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.covidmobileapp:layout/activity_main: Binary XML file line #20 in com.example.covidmobileapp:layout/content_main: Error inflating class fragment

Which seems to spot errors in 2 xml files I have: activity_main.xml and content_main.xml这似乎发现了我拥有的 2 个 xml 文件中的错误: activity_main.xmlcontent_main.xml

Goind down the stack trace it seems that the error hits when the app view is created沿着堆栈跟踪下去,似乎在创建应用程序视图时出现了错误

at com.example.covidmobileapp.MainActivity.onCreate(MainActivity.java:55)

This is my code for the files above:这是我上面文件的代码:

MainActivity.java MainActivity.java

package com.example.covidmobileapp;

import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;


import com.example.covidmobileapp.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {



    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow , R.id.nav_homepage)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        setContentView(R.layout.activity_main);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }





}

content_main.xml I have commented the line where the fragment error hits content_main.xml 我已经注释了片段错误命中的行

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" //this is where the fragment error hits 
  />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml I have also commented where the error is here activity_main.xml 我也评论了错误在哪里

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /> //this is where error for activity_main is 

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

I would appreciate your help as this is really frustrating.我会很感激你的帮助,因为这真的很令人沮丧。 If more info is needed I will upload it.如果需要更多信息,我会上传。

I think you should declare:我认为你应该声明:

    NavigationView leftNavigationView;

in your main activity and get you nav view in on create method:在您的主要活动中并让您在创建方法中导航视图:

        leftNavigationView = findViewById(R.id.nav_view);
        setLeftNavigationView();

Then you havae to use setNavigationItemSelectedListener.然后你必须使用 setNavigationItemSelectedListener。 You can create a method like this:您可以创建这样的方法:

private void setLeftNavigationView() {
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this,
            drawerLayout,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    leftNavigationView.setNavigationItemSelectedListener(item -> {

        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.nav_yourFragment:
                selectedFragment = new YourFragment();
                drawerLayout.closeDrawer(GravityCompat.START);
                break;
    }
getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, selectedFragment).addToBackStack("tag")
                .commit();
        return true;
});

and so on.等等。 R.id.nav_yourFragment is your item from your menu Also you have to put a FrameLayout container in your xml file: R.id.nav_yourFragment 是您菜单中的项目您还必须在 xml 文件中放置一个 FrameLayout 容器:

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

I hope this is useful for you我希望这对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.view.InflateException夸大类片段的错误 - android.view.InflateException Error inflating class fragment 错误夸大类片段:android.view.InflateException - Error inflating class fragment: android.view.InflateException Android调试错误android.view.InflateException:二进制XML文件第9行:夸大类片段的错误 - Android debugging error android.view.InflateException: Binary XML file line #9: Error inflating class fragment Android Google Map API v2-错误扩充类别片段android.view.InflateException - Android Google Map API v2 - Error Inflating Class Fragment android.view.InflateException 原因:android.view.InflateException膨胀Android应用程序中的类时出错 - Caused by: android.view.InflateException Error inflating class in Android app Android-android.view.InflateException:二进制XML文件第9行:膨胀类片段的错误 - Android - android.view.InflateException: Binary XML file line #9: Error inflating class fragment 谷歌地图视图android.view.InflateException:二进制XML文件-错误夸大类片段 - Google maps view android.view.InflateException: Binary XML File - error inflating class fragment 我遇到错误,显示通过 android.view.InflateException 对 class 片段进行膨胀时出错:二进制 XML 文件 - i am having error that shows Error inflating class fragment by android.view.InflateException: Binary XML file android.view.InflateException:二进制XML文件行#8:错误膨胀类片段 - android.view.InflateException: Binary XML file line #8: Error inflating class fragment android.view.InflateException:二进制XML文件第0行:二进制XML文件第0行:膨胀类片段时出错 - android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM