Как решить ошибку `Попытка вызвать виртуальный метод 'void androidx.appcompat.app.ActionBar.setTitle (ja ......`? - PullRequest
0 голосов
/ 02 мая 2020

Я пытаюсь создать приложение навигатора, но я столкнулся с ошибкой

MainActivity.kt

        val navController = findNavController(R.id.my_nav_host_fragment)
        val drawerLayout=findViewById<DrawerLayout>(R.id.drawer_layout)
        nav_view.setupWithNavController(navController)
        val appBarConfiguration = AppBarConfiguration(navController.graph,drawerLayout)
        setupActionBarWithNavController(navController,appBarConfiguration)

Это мой файл манифеста AndroidManifest . xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.navigationdrawer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"

        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Стили. xml

<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>
    </style>

Я чувствую, что ошибка находится где-то в вышеуказанных файлах, однако я даю activity_main. xml

<androidx.drawerlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/drawer_layout">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:title="toolbar"
            app:menu="@menu/overflow_menu"
            app:titleMarginStart="40dp" />
    </com.google.android.material.appbar.AppBarLayout>

    <fragment
            android:id="@+id/my_nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"

            app:defaultNavHost="true"

            app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/main_toolbar">
    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

Теперь об ошибке

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer/com.example.navigationdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference

Теперь, если я заменю код setupActionBarWithNavController(navController,appBarConfiguration) This is my manifest file AndroidManifest.xml на setSupportActionBar(findViewById(R.id.toolbar)) NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration)

Ошибка не появляется, но когда я нажимаю на иконку, она не работает, но, тем не менее, когда я ее сдвигаю, она работает.

...