使用Kotlin作为底部导航组件创建了一个应用程序,但启动后每次都会崩溃。
- 主要活动**
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNav = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
val fragmentContainer = findViewById<View>(R.id.fragmentContainer)
bottomNav.setupWithNavController(fragmentContainer.findNavController())
}
}
- 导航. xml**
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation"
app:startDestination="@id/kotlinFragment">
<fragment
android:id="@+id/kotlinFragment"
android:name="com.example.bottomnavigation.ui.KotlinFragment"
android:label="fragment_kotlin"
tools:layout="@layout/fragment_kotlin" />
<fragment
android:id="@+id/javaFragment"
android:name="com.example.bottomnavigation.ui.JavaFragment"
android:label="fragment_java"
tools:layout="@layout/fragment_java" />
<fragment
android:id="@+id/flutterFragment"
android:name="com.example.bottomnavigation.ui.FlutterFragment"
android:label="fragment_flutter"
tools:layout="@layout/fragment_flutter" />
</navigation>
- 底部导航. xml**
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="@string/kotlin"
android:id="@+id/kotlinFragment"
android:icon="@drawable/code"/>
<item
android:title="@string/java"
android:id="@+id/javaFragment"
android:icon="@drawable/code"/>
<item
android:title="@string/flutter"
android:id="@+id/flutterFragment"
android:icon="@drawable/code"/>
</menu>
- 崩溃后出错**
E/libc: Access denied finding property "ro.odm.prev.product.name"
I/Perf: Connecting to perf service.
E/Perf: perftest packageName : com.example.bottomnavigation App is allowed to use Hide APIs
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
E/libEGL: Invalid file path for libcolorx-loader.so
E/MultiApp.Impl: OplusMultiAppImpl
D/AppCompatDelegate: Checking for metadata for AppLocalesMetadataHolderService : Service not found
I/oplus.android.OplusFrameworkFactoryImpl: getOplusRedPacketManager
W/ottomnavigatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/ottomnavigatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
E/OplusCustomizeRestrictionManager: sInstance is null, start a new sInstance
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bottomnavigation, PID: 9514
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bottomnavigation/com.example.bottomnavigation.MainActivity}: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{13e6350 V.E...... ......I. 0,0-0,0 #7f0800c5 app:id/fragmentContainer} does not have a NavController set
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3729)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:263)
at android.app.ActivityThread.main(ActivityThread.java:8296)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
Caused by: java.lang.IllegalStateException: View androidx.fragment.app.FragmentContainerView{13e6350 V.E...... ......I. 0,0-0,0 #7f0800c5 app:id/fragmentContainer} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.kt:71)
at androidx.navigation.ViewKt.findNavController(View.kt:28)
at com.example.bottomnavigation.MainActivity.onCreate(MainActivity.kt:18)
at android.app.Activity.performCreate(Activity.java:8153)
at android.app.Activity.performCreate(Activity.java:8137)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3698)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3896)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2326)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:263)
at android.app.ActivityThread.main(ActivityThread.java:8296)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
I/Process: Sending signal. PID: 9514 SIG: 9
我做了3个菜单项"Kotlin","java"和"flutter",使用底部导航在片段之间切换。使用Android Studio(Kotlin)创建应用程序
1条答案
按热度按时间dgiusagp1#
试试看
和bottomnav.xml删除@+id/