Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我正在尝试创建导航抽屉,但出现了此错误_Java_Android_Xml - Fatal编程技术网

Java I';我正在尝试创建导航抽屉,但出现了此错误

Java I';我正在尝试创建导航抽屉,但出现了此错误,java,android,xml,Java,Android,Xml,当我向导航抽屉添加标题时,出现了此错误 android.view.InflateException:二进制XML文件行#0:错误 膨胀类android.support.design.widget.NavigationView但是 没有标题,它可以正常工作 第一个文件是MainActivity它包含四个片段和底部导航 第二个是acivity\u main.xml布局 我认为错误可能在styles.xml文件中,这就是为什么我也把它放进去了 public class MainActivity ex

当我向导航抽屉添加标题时,出现了此错误

android.view.InflateException:二进制XML文件行#0:错误 膨胀类android.support.design.widget.NavigationView但是 没有标题,它可以正常工作

第一个文件是
MainActivity
它包含四个片段和底部导航 第二个是
acivity\u main.xml
布局 我认为错误可能在
styles.xml
文件中,这就是为什么我也把它放进去了

 public class MainActivity extends AppCompatActivity {
    private BottomNavigationView.OnNavigationItemSelectedListener 
    mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    setTitle("Home");
                    HomeFragment fragmentTasks = new HomeFragment();
                    FragmentTransaction fragmentTransactionTasks = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionTasks.replace(R.id.frame_layout, fragmentTasks);
                    fragmentTransactionTasks.commit();

                    return true;
                case R.id.notification:
                    setTitle("Notification");
                    NoteFragment fragmentQA = new NoteFragment();
                    FragmentTransaction fragmentTransactionQa = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionQa.replace(R.id.frame_layout, fragmentQA);
                    fragmentTransactionQa.commit();

                    return true;
                case R.id.profile:
                    setTitle("Profile");
                    ProFragment fragmentCashOut = new ProFragment();
                    FragmentTransaction fragmentTransactionCashout = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionCashout.replace(R.id.frame_layout, fragmentCashOut);
                    fragmentTransactionCashout.commit();

                    return true;
                case R.id.help:
                    setTitle("Help");
                    HelpFragment fragmentDiscuss = new HelpFragment();
                    FragmentTransaction fragmentTransactionDiscuss = getSupportFragmentManager().beginTransaction();
                    fragmentTransactionDiscuss.replace(R.id.frame_layout, fragmentDiscuss);
                    fragmentTransactionDiscuss.commit();

                    return true;
            }
            return false;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navigation =findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        setTitle("Home");

        HomeFragment fragmentTasks = new HomeFragment();
        FragmentTransaction fragmentTransactionTasks = getSupportFragmentManager().beginTransaction();
        fragmentTransactionTasks.replace(R.id.frame_layout, fragmentTasks);
        fragmentTransactionTasks.commit();
    }
}

//activity_main.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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.votingsystemv1.Activities.MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary"
            app:menu="@menu/bottombar_menu"
            app:itemTextColor="@color/colorIcons"
            app:itemIconTint="@color/colorIcons">

        </android.support.design.widget.BottomNavigationView>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
    app:itemTextColor="@color/colorPrimaryText"

    app:menu="@menu/drawer_menu"
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    android:fitsSystemWindows="true"    />


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

//nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="170dp"
    android:background="@color/colorPrimary"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/iv_display_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/no_image"
        app:civ_border_color="@color/colorGrey"
        app:civ_border_width="1dp"/>

    <TextView
        android:id="@+id/tv_display_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        tools:text="Yousra Mahdi"/>


</LinearLayout>
//style.xml

    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>


    <style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="Base.ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppTheme.ErrorTextAppearance" parent="TextAppearance.AppCompat">
        <item name="android:textAppearance">@style/TextAppearance.AppCompat.Caption</item>
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

    <!-- Fullscreen application theme. -->
    <style name="FullscreenTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

    <style name="Theme.MyDialog" parent="@style/Theme.AppCompat.Light.Dialog">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


    <style name="AppTheme.Dark.Dialog" parent="Theme.AppCompat.Dialog">
        <item name="colorAccent">@color/colorIcons</item>
        <item name="android:textColorPrimary">@color/colorAccent</item>
        <item name="android:background">@color/colorGrey</item>
    </style>


</resources>
public类MainActivity扩展了AppCompatActivity{
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
=新的BottomNavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(@NonNull MenuItem item){
开关(item.getItemId()){
案例R.id.home:
房屋所有权(“房屋”);
HomeFragment fragmentTasks=新建HomeFragment();
FragmentTransaction fragmentTransactionTasks=getSupportFragmentManager().beginTransaction();
fragmentTransactionTasks.replace(R.id.frame_布局,fragmentTasks);
fragmentTransactionTasks.commit();
返回true;
案例R.id.通知:
设定所有权(“通知”);
NoteFragment fragmentQA=新的NoteFragment();
FragmentTransaction fragmentTransactionQa=getSupportFragmentManager().beginTransaction();
碎片交易QA.更换(R.id.frame_布局,碎片QA);
fragmentTransactionQa.commit();
返回true;
案例R.id.profile:
片名(“简介”);
ProFragment fragmentCashOut=新的ProFragment();
FragmentTransaction fragmentTransactionCashout=getSupportFragmentManager().beginTransaction();
fragmentTransactionCashout.替换(R.id.frame_布局,fragmentCashOut);
fragmentTransactionCashout.commit();
返回true;
案例R.id.help:
设置标题(“帮助”);
HelpFragment FragmentDiscussion=新的HelpFragment();
FragmentTransaction FragmentTransaction Discussion=getSupportFragmentManager().beginTransaction();
碎片交易讨论。替换(R.id.frame_布局,碎片讨论);
FragmentTransactionDiscussion.commit();
返回true;
}
返回false;
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView导航=findViewById(R.id.navigation);
setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
房屋所有权(“房屋”);
HomeFragment fragmentTasks=新建HomeFragment();
FragmentTransaction fragmentTransactionTasks=getSupportFragmentManager().beginTransaction();
fragmentTransactionTasks.replace(R.id.frame_布局,fragmentTasks);
fragmentTransactionTasks.commit();
}
}
//activity_main.xml
//nav_header.xml
//style.xml
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的
真的
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
假的
真的
@style/TextAppearance.AppCompat.Caption
@颜色/原色
假的
真的
假的
真的
假的
真的
@颜色/颜色图标
@颜色/颜色重音
@颜色/颜色灰

我认为您应该尝试注释
CircleImageView
和textview,并测试它是否有效。nav_标题中的某些子视图可能存在膨胀问题

我已经在我的IDE上试用了你的代码,它运行得非常好