当我在主 activity 布局中添加片段并添加代码时,应用程序崩溃
App is crashing when I add fragment in main activity layout and add code of it
我已经创建了一个包含底部导航的应用程序,但是当我尝试在 MAin 中添加片段时 Activity 或者添加关于片段切换的代码然后它会崩溃。
我正在添加 activity_main.XML、MainActivity.main 和 Build.gradle、日志 还添加了
activity_main.XML
<androidx.constraintlayout.widget.ConstraintLayout 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">
<fragment
android:id="@+id/frame_layout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.Main
package com.androtechbuddy.collegetest;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import java.util.Objects;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
navController = Navigation.findNavController(this, R.id.frame_layout);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
Build.gradle
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.androtechbuddy.collegeapptest"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'com.google.android.material:material:1.4.0-alpha02'
implementation 'com.github.smarteist:autoimageslider:1.2.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.google.firebase:firebase-analytics:18.0.3'
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.google.firebase:firebase-database:19.7.0'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
implementation 'com.facebook.shimmer:shimmer:0.5.0'
}
日志
2021-04-29 23:27:28.487 1300-1300/com.androtechbuddy.collegeapptest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androtechbuddy.collegeapptest, PID: 1300
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androtechbuddy.collegeapptest/com.androtechbuddy.collegeapptest.MainActivity}: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3501)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3661)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2108)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7876)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.androtechbuddy.collegeapptest:id/mobile_navigation
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:61)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:28)
at androidx.navigation.NavController.navigate(NavController.java:1066)
at androidx.navigation.NavController.onGraphCreated(NavController.java:639)
at androidx.navigation.NavController.setGraph(NavController.java:592)
at androidx.navigation.NavController.setGraph(NavController.java:557)
at androidx.navigation.NavController.setGraph(NavController.java:539)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:248)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:109)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1071)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:999)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
at android.view.LayoutInflater.inflate(LayoutInflater.java:684)
at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.androtechbuddy.collegeapptest.MainActivity.onCreate(MainActivity.java:30)
2021-04-29 23:27:28.487 1300-1300/com.androtechbuddy.collegeapptest E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:8093)
at android.app.Activity.performCreate(Activity.java:8067)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3474)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3661)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2108)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7876)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
2021-04-29 23:27:28.487 943-943/? E/qdmetadata: paramType 2048 not supported
谁能告诉我我的代码有什么问题吗?以及如何解决这个问题。
错误显示 Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.androtechbuddy.collegeapptest:id/mobile_navigation
。
您需要检查 res/navigation
文件夹中的 mobile_navigation.xml
文件,以确保您已经在 <navigation>
元素上定义了一个 app:startDestination
android:id="@+id/mobile_navigation"
根据 Designate a start destination step of the Getting Started guide.
我已经创建了一个包含底部导航的应用程序,但是当我尝试在 MAin 中添加片段时 Activity 或者添加关于片段切换的代码然后它会崩溃。
我正在添加 activity_main.XML、MainActivity.main 和 Build.gradle、日志 还添加了
activity_main.XML
<androidx.constraintlayout.widget.ConstraintLayout 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">
<fragment
android:id="@+id/frame_layout"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.Main
package com.androtechbuddy.collegetest;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import java.util.Objects;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
navController = Navigation.findNavController(this, R.id.frame_layout);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
navigationView.setNavigationItemSelectedListener(this);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
Build.gradle
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
id 'com.google.firebase.crashlytics'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.androtechbuddy.collegeapptest"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'com.google.android.material:material:1.4.0-alpha02'
implementation 'com.github.smarteist:autoimageslider:1.2.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'com.google.firebase:firebase-analytics:18.0.3'
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.google.firebase:firebase-database:19.7.0'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
implementation 'com.facebook.shimmer:shimmer:0.5.0'
}
日志
2021-04-29 23:27:28.487 1300-1300/com.androtechbuddy.collegeapptest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androtechbuddy.collegeapptest, PID: 1300
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androtechbuddy.collegeapptest/com.androtechbuddy.collegeapptest.MainActivity}: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3501)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3661)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2108)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7876)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #8 in com.androtechbuddy.collegeapptest:layout/activity_main: Error inflating class fragment
Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.androtechbuddy.collegeapptest:id/mobile_navigation
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:61)
at androidx.navigation.NavGraphNavigator.navigate(NavGraphNavigator.java:28)
at androidx.navigation.NavController.navigate(NavController.java:1066)
at androidx.navigation.NavController.onGraphCreated(NavController.java:639)
at androidx.navigation.NavController.setGraph(NavController.java:592)
at androidx.navigation.NavController.setGraph(NavController.java:557)
at androidx.navigation.NavController.setGraph(NavController.java:539)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:248)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1175)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:109)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:335)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1071)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:999)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1125)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1086)
at android.view.LayoutInflater.inflate(LayoutInflater.java:684)
at android.view.LayoutInflater.inflate(LayoutInflater.java:536)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.androtechbuddy.collegeapptest.MainActivity.onCreate(MainActivity.java:30)
2021-04-29 23:27:28.487 1300-1300/com.androtechbuddy.collegeapptest E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:8093)
at android.app.Activity.performCreate(Activity.java:8067)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3474)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3661)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2108)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:7876)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
2021-04-29 23:27:28.487 943-943/? E/qdmetadata: paramType 2048 not supported
谁能告诉我我的代码有什么问题吗?以及如何解决这个问题。
错误显示 Caused by: java.lang.IllegalStateException: no start destination defined via app:startDestination for com.androtechbuddy.collegeapptest:id/mobile_navigation
。
您需要检查 res/navigation
文件夹中的 mobile_navigation.xml
文件,以确保您已经在 <navigation>
元素上定义了一个 app:startDestination
android:id="@+id/mobile_navigation"
根据 Designate a start destination step of the Getting Started guide.