空指针异常,在构建和运行 bottomnavigation 活动时发生。

huangapple go评论70阅读模式
英文:

Null pointer exception while building and running bottomnavigation activity

问题

我在片段中有一个“按钮”,点击该“按钮”会将我带到一个具有“底部导航”的新活动中。

按钮:

<androidx.appcompat.widget.AppCompatButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/driveBtn"
    android:layout_below="@id/recyclerview"
    android:layout_marginTop="10sp"
    android:text="预订出租车"
    android:background="@drawable/buttonshape"
    android:layout_marginLeft="16sp"
    android:layout_marginRight="16sp"
    android:textColor="@color/white" />

重定向到新活动的代码:

case R.id.driveBtn:
    Intent intent3 = new Intent(getContext(), DriveActivity.class);
    startActivity(intent3);
    break;

DriveActivity:

public class DriveActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drive);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

}

activity_drive:

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"
    tools:context=".view.ui._activity.DriveActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        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/bottom_nav_menu"
        app:labelVisibilityMode="labeled" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我尝试运行该应用时,我收到以下错误消息:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)
From where is that 'ActionBar' being called?
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227)
at com.dell.mycampus.view.ui._activity.DriveActivity.onCreate(DriveActivity.java:27)

英文:

I've a button in a fragment, on click of that button leads me to a new activity which has bottomnavigation.
Button:

&lt;androidx.appcompat.widget.AppCompatButton
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:id=&quot;@+id/driveBtn&quot;
        android:layout_below=&quot;@id/recyclerview&quot;
        android:layout_marginTop=&quot;10sp&quot;
        android:text=&quot;Book a Cab&quot;
        android:background=&quot;@drawable/buttonshape&quot;
        android:layout_marginLeft=&quot;16sp&quot;
        android:layout_marginRight=&quot;16sp&quot;
        android:textColor=&quot;@color/white&quot; /&gt;  

Code to redirect to new activity:

case R.id.driveBtn:
                Intent intent3 = new Intent(getContext(), DriveActivity.class);
                startActivity(intent3);  
                break;  

DriveActivity:

public class DriveActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drive);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

}

activity_drive:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/container&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:paddingTop=&quot;?attr/actionBarSize&quot;
    tools:context=&quot;.view.ui._activity.DriveActivity&quot;&gt;

    &lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:id=&quot;@+id/nav_view&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;0dp&quot;
        android:layout_marginEnd=&quot;0dp&quot;
        android:background=&quot;?android:attr/windowBackground&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        app:menu=&quot;@menu/bottom_nav_menu&quot;
        app:labelVisibilityMode=&quot;labeled&quot; /&gt;

    &lt;fragment
        android:id=&quot;@+id/nav_host_fragment&quot;
        android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        app:defaultNavHost=&quot;true&quot;
        app:layout_constraintBottom_toTopOf=&quot;@id/nav_view&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:navGraph=&quot;@navigation/mobile_navigation&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;  

When I try running that app, I get this error:
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at androidx.navigation.ui.ActionBarOnDestinationChangedListener.setTitle(ActionBarOnDestinationChangedListener.java:48)
at androidx.navigation.ui.AbstractAppBarOnDestinationChangedListener.onDestinationChanged(AbstractAppBarOnDestinationChangedListener.java:103)
From where is that ActionBar being called?
at androidx.navigation.NavController.addOnDestinationChangedListener(NavController.java:233)
at androidx.navigation.ui.NavigationUI.setupActionBarWithNavController(NavigationUI.java:227)
at com.dell.mycampus.view.ui._activity.DriveActivity.onCreate(DriveActivity.java:27)

答案1

得分: 1

Theme.AppCompat.Light.Dialog.Alert是适用于Dialog的主题。具体来说,适用于AlertDialog。对话框没有操作栏,因此预期使用该主题的活动将没有操作栏,导致getSupportActionBar()始终返回null。

当你调用setupActionBarWithNavController()时,你是想要在你的NavController中设置操作栏。由于没有操作栏,预期每次调用setTitlesetDisplayHomeAsUp()以及其他ActionBar API都会失败并引发NullPointerException

如果你想要一个默认包含操作栏的主题,你应该为你的活动使用Theme.AppCompat.Light主题。

英文:

Theme.AppCompat.Light.Dialog.Alert is a theme appropriate for a Dialog. Specifically, an AlertDialog. Dialogs do not have an action bar, so it is expected that an Activity using that theme will not have an action bar, resulting in getSupportActionBar() always returning null.

When you say setupActionBarWithNavController() you are saying you want to set up your action bar with your NavController. Since have no action bar, it is expected that every call to setTitle, setDisplayHomeAsUp() and other ActionBar API fail with a NullPointerException.

If you want a theme that includes an action bar by default, you should use a Theme.AppCompat.Light theme for your activity.

huangapple
  • 本文由 发表于 2020年9月28日 06:32:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64093919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定