底部导航不起作用,并导致应用崩溃

时间:2019-04-05 18:57:39

标签: android xml android-layout bottomnavigationview

我尝试在我的应用程序上使用底部导航,但是它不起作用,并且应用程序崩溃。 这是我的主要活动:

public static String getStringFromSubscription(Subscription subscription) throws MbException {
    Marshaller marshaller;
    StringWriter stringWriter = new StringWriter();
    try {
        marshaller = JAXBContext.newInstance("com.myhealth.com").createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        marshaller.marshal(subscription, stringWriter);
    } catch (Exception e) {
        throw new MbException(e);
    }
    return stringWriter;
}

这是我的xml文件:

package com.example.testing;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

        @Override
        protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            loadFragment(new HomeFragment());
            BottomNavigationView navigation = findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(this);
    }

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;

            switch (item.getItemId()) {
                case R.id.homePage:
                    fragment = new HomeFragment();
                    break;

                case R.id.chat:
                    fragment = new ChatFragment();
                    break;
            }

            return loadFragment(fragment);
        }

        private boolean loadFragment(Fragment fragment) {
            if (fragment != null) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, fragment)
                        .commit();
                return true;
            }
            return false;
        }
    }

此外,我为导航创建了两个片段,它们都具有相同的xml和类代码

片段xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/menu_nav" />


</android.support.constraint.ConstraintLayout>

片段类:

<?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="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>

我还将此代码添加到了build.gradle

package com.example.testing;

public class HomeFragment extends Fragment {
    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, null);

    }
}

但是我在logcat中得到了什么:

implementation 'com.android.support:design:28.0.0'

请帮助

HERE是菜单xml:

    --------- beginning of crash
2019-04-05 21:55:34.150 3169-3169/com.example.testing E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testing, PID: 3169
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        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 #20: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
     Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
     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.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.example.testing.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        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: Couldn't resolve menu item onClick handler chat in class com.example.testing.MainActivity
        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:254)
        at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:482)
        at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:530)
        at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:206)
        at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:128)
        at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:252)
        at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:187)
2019-04-05 21:55:34.152 3169-3169/com.example.testing E/AndroidRuntime:     at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
            ... 26 more
     Caused by: java.lang.NoSuchMethodException: chat [interface android.view.MenuItem]
        at java.lang.Class.getMethod(Class.java:2068)
        at java.lang.Class.getMethod(Class.java:1690)
        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:250)
            ... 33 more

2 个答案:

答案 0 :(得分:0)

Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler chat in class com.example.testing.MainActivity

删除点击次数

答案 1 :(得分:0)

问题出在菜单XML文件中的这一行-

android:onClick="chat"

这试图引用方法public void chat(View),该方法将处理菜单项的click事件。但是,您的活动中没有定义此类方法,因此应用程序崩溃,因为它找不到所需的方法。与其他菜单项相同,除了它崩溃之前甚至还没有崩溃。

解决方案?由于您已经为底部导航菜单实现了OnClick,因此请从菜单项中删除onClick属性。