我的Android应用由于导航栏而停止

时间:2019-03-29 09:49:47

标签: android

我已经创建了一个导航栏,但是出现了一些错误,并且活动主页没有响应; logCat中的错误部分:

....
2019-03-29 12:31:32.612 6437-6437/com.example.mashorah E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mashorah, PID: 6437
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mashorah/com.example.mashorah.HomePage}: android.view.InflateException: Binary XML file line #138: Binary XML file line #138: 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 #138: Binary XML file line #138: Error inflating class android.support.design.widget.BottomNavigationView
     Caused by: android.view.InflateException: Binary XML file line #138: 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.mashorah.HomePage.onCreate(HomePage.java:21)
        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 profile in class com.example.mashorah.HomePage
        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-03-29 12:31:32.624 6437-6437/com.example.mashorah E/AndroidRuntime:     at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
            ... 26 more
     Caused by: java.lang.NoSuchMethodException: profile [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
2019-03-29 12:31:32.850 6437-6448/com.example.mashorah I/zygote: NativeAlloc concurrent copying GC freed 1503(235KB) AllocSpace objects, 1(20KB) LOS objects, 51% free, 1448KB/2MB, paused 22us total 218.328ms

这是我的主页XML文件;导航栏部分:

<FrameLayout
    android:id="@+id/Fragment_content"
    android:layout_width="match_parent"
    android:layout_height="715dp"
    android:layout_above="@+id/NavBot"
    android:layout_alignParentStart="true"
    android:layout_marginStart="0dp"
    android:layout_marginBottom="-40dp" />


<android.support.design.widget.BottomNavigationView
    android:id="@+id/NavBot"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="0dp"
    android:layout_marginBottom="5dp"
    app:itemTextColor="@color/colorPrimary"
    app:menu="@menu/menu_nav" />

我在新目录中添加了一个导航菜单,其中包含三项内容,其中包含类型菜单,其向量位于可绘制文件中

这是我的主页活动:

package com.example.mashorah;

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


public class HomePage extends AppCompatActivity {

    ImageView edu , eco , med, law , fml ;

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

        edu = (ImageView) findViewById(R.id.edu);
        eco = (ImageView) findViewById(R.id.eco);
        law = (ImageView) findViewById(R.id.law);
        med = (ImageView) findViewById(R.id.med);
        fml = (ImageView) findViewById(R.id.family);

        BottomNavigationView bottomNAv = findViewById(R.id.NavBot);
        bottomNAv.setOnNavigationItemSelectedListener(navListener);
    }

    public void econClick(View v){
        Intent econInt = new Intent(HomePage.this , econConsult.class) ;
        startActivity(econInt);
    }

    public void eduClick(View v){
        Intent eduInt = new Intent(HomePage.this , eduConsult.class) ;
        startActivity(eduInt);
    }

    public void medClick(View v){
        Intent medInt = new Intent(HomePage.this , medConsult.class) ;
        startActivity(medInt);
    }

    public void fmlyClick(View v){
        Intent fmlyInt = new Intent(HomePage.this , FmlyConsult.class) ;
        startActivity(fmlyInt);
    }

    public void lawClick(View v){
        Intent lowInt = new Intent(HomePage.this , lawConsult.class) ;
        startActivity(lowInt);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null ;

                    switch (menuItem.getItemId()){

                        case R.id.homePage:
                            selectedFragment = new HomeFragment() ;
                            break;

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

                        case R.id.profile:
                            selectedFragment = new ProfileFragment() ;
                            break;
                    }

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

2 个答案:

答案 0 :(得分:0)

您需要将此行添加到build.gradle文件中。我认为您所在的行不存在。

dependencies {
    ...
    implementation 'com.android.support:design:27.1.0'
}

这就是为什么xml无法膨胀的原因。 Gradle无法找到BottomNavigationView

最佳

答案 1 :(得分:0)

确保您的appcompat库和design support库具有相同的版本。