gpt4 book ai didi

java - BottomNavigationViewEx 空指针异常

转载 作者:行者123 更新时间:2023-12-01 07:46:14 24 4
gpt4 key购买 nike

我的 xml 文件包含代码:

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bnve"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@drawable/bottom_navigation_colors"
app:menu="@menu/bottom_nav_menu" />

我的java文件包含:

BottomNavigationViewEx bnve = (BottomNavigationViewEx)findViewById(R.id.bnve);

它可以在 Android 设备上完美运行。但是当我添加以下行时:

    bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);

应用程序崩溃并出现错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bimsina.projectchautari/com.bimsina.projectchautari.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5765) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.getBottomNavigationItemViews(BottomNavigationViewEx.java:569) at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(BottomNavigationViewEx.java:341) at com.bimsina.projectchautari.MainActivity.onCreate(MainActivity.java:94) at android.app.Activity.performCreate(Activity.java:6331) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)

有人有解决办法吗?

编辑:这是我的整个 xml 和 java 文件。

  <?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bnve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu"/>

</android.support.constraint.ConstraintLayout>

java 文件是:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class MainActivity extends AppCompatActivity {

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

BottomNavigationViewEx bnve= findViewById(R.id.bnve);
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
}
}

最佳答案

此错误来自于使用与该库不兼容的不同版本的支持库。 因此对于旧版本支持 lib 25 或 26 使用 com.github.ittianyu:BottomNavigationViewEx:1.2.4

以及新的 com.android.support:design:28.0.0 使用 com.github.ittianyu:BottomNavigationViewEx:2.0.2

这样就可以解决问题了

关于java - BottomNavigationViewEx 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51618340/

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