我刚刚将 Android Studio 更新到 v3.5,我的项目停止运行,给了我以下异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mcampana.turle/com.mcampana.turle.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
Caused by: java.lang.RuntimeException: Exception inflating com.mcampana.turle:navigation/nav_graph line 23
at androidx.navigation.NavInflater.inflate(NavInflater.java:90)
at androidx.navigation.NavController.setGraph(NavController.java:436)
at androidx.navigation.NavController.setGraph(NavController.java:418)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:236)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2655)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1278)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1581)
at androidx.fragment.app.FragmentManager.addFragment(FragmentManager.java:1840)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:104)
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.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.mcampana.turle.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
据我了解,Android 引发了这个异常,因为它无法在我的 MainActivity 中膨胀导航图......但自上次运行以来我没有改变任何东西!
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
activity_main.xml
<?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"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_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:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
nav_graph.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/nav_graph"
app:startDestination="@id/navigation_home">
<fragment
android:id="@id/navigation_home"
android:name="com.mcampana.turle.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@id/navigation_restaurants"
android:name="com.mcampana.turle.RestaurantsFragment"
android:label="fragment_restaurants"
tools:layout="@layout/fragment_restaurants" >
<action
android:id="@+id/action_to_map"
app:destination="@id/mapFragment"
app:enterAnim="@anim/fragment_open_enter"
app:popEnterAnim="@anim/fragment_fade_enter" />
<argument
android:name="restaurant"
android:defaultValue="null"
app:argType="com.mcampana.turle.model.Restaurant" />
</fragment>
<fragment
android:id="@id/navigation_profile"
android:name="com.mcampana.turle.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<action
android:id="@+id/action_login"
app:destination="@id/loginFragment" />
<action
android:id="@+id/action_userprofile"
app:destination="@id/userProfileFragment" />
</fragment>
<fragment
android:id="@+id/registerFragment"
android:name="com.mcampana.turle.RegisterFragment"
android:label="fragment_register"
tools:layout="@layout/fragment_register" />
<fragment
android:id="@+id/loginFragment"
android:name="com.mcampana.turle.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_register"
app:destination="@id/registerFragment" />
</fragment>
<fragment
android:id="@+id/userProfileFragment"
android:name="com.mcampana.turle.UserProfileFragment"
android:label="fragment_user_profile"
tools:layout="@layout/fragment_user_profile" >
<action
android:id="@+id/action_qrfragment"
app:destination="@id/QRCodeFragment" />
</fragment>
<fragment
android:id="@+id/QRCodeFragment"
android:name="com.mcampana.turle.QRCodeFragment"
android:label="fragment_qrcode"
tools:layout="@layout/fragment_qrcode" />
<fragment
android:id="@+id/mapFragment"
android:name="com.mcampana.turle.MapFragment"
android:label="fragment_map"
tools:layout="@layout/fragment_map" >
<action
android:id="@+id/action_map_to_restaurant"
app:destination="@id/navigation_restaurants" />
</fragment>
</navigation>