当我切换到包含片段的活动时应用程序崩溃(二进制 XML 文件第 10 行:二进制 XML 文件第 10 行:膨胀类片段时出错)
Posted
技术标签:
【中文标题】当我切换到包含片段的活动时应用程序崩溃(二进制 XML 文件第 10 行:二进制 XML 文件第 10 行:膨胀类片段时出错)【英文标题】:App crashes when I switch to an activity that contains a fragment (Binary XML file line #10: Binary XML file line #10: Error inflating class fragment) 【发布时间】:2021-06-18 14:54:35 【问题描述】:当我按下按钮打开另一个包含片段的 Activity 时,我的应用程序崩溃了。我是 android Studio 新手,似乎找不到任何解决方案。
在 activity_main.xml 中,我删除了这个问题的其他按钮,因为我想主要关注这个 monday_btn。
我研究了这种类型的错误并尝试了许多技巧,但似乎都没有奏效。
MainActivity.java
package com.example.dietmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Intent i = getIntent();
String current_user = i.getStringExtra("current_user");
TextView current_user_txtview = findViewById(R.id.current_user);
current_user_txtview.setText("Welcome, " + current_user);*/
Button monday = findViewById(R.id.monday_btn);
monday.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent day = new Intent(MainActivity.this, DayActivity.class);
startActivity(day);
);
DayActivity.java
package com.example.dietmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
public class DayActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day);
FragmentTransaction fragmentTransaction;
fragmentTransaction = getSupportFragmentManager().beginTransaction();
DayFragment dayFragment = new DayFragment();
fragmentTransaction.add(R.id.dayfragment, dayFragment);
fragmentTransaction.commit();
DayFragment.java
package com.example.dietmanagement;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class DayFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_day, container, false);
return view;
fragment_day.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_
android:layout_
android:background="@color/background_green"
tools:context=".DayFragment">
<TextView
android:id="@+id/day"
android:layout_
android:layout_
android:layout_marginTop="24dp"
android:text="@string/day" />
</FrameLayout>
activity_day.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:layout_
android:layout_
android:background="@color/background_green"
tools:context=".DayActivity">
<fragment
android:id="@+id/dayfragment"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</fragment>
</androidx.constraintlayout.widget.ConstraintLayout>
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:layout_
android:layout_
android:background="@color/background_green"
tools:context=".MainActivity">
<Button
android:id="@+id/monday_btn"
android:layout_
android:layout_
android:text="@string/monday"
android:textColor="#6FED84"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.091"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_title"
app:layout_constraintVertical_bias="0.082" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我收到的错误消息:
2021-03-21 23:34:35.459 29479-29479/com.example.dietmanagement E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dietmanagement, PID: 29479
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.dietmanagement/com.example.dietmanagement.DayActivity: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
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.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:617)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3602)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6187)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
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:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.dietmanagement.DayActivity.onCreate(DayActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
【问题讨论】:
【参考方案1】:您正在尝试以两种不同的方式加载片段 - 直接通过带有 <fragment>
标记的布局,这需要您指定 哪个 您实际要加载的片段,并且作为需要在其中放置片段的布局的活动中的事务。
// This says - "hey Android load up a fragment for me", but you don't say which
<fragment
android:id="@+id/dayfragment"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</fragment>
但是……
// This says - "hey Android, shove the new "dayFragment" into the view R.id.dayFragment".
// But based on your previous XML declaration, R.id.dayFragment is itself already a
// Fragment!
FragmentTransaction fragmentTransaction;
fragmentTransaction = getSupportFragmentManager().beginTransaction();
DayFragment dayFragment = new DayFragment();
fragmentTransaction.add(R.id.dayfragment, dayFragment);
fragmentTransaction.commit();
根据您发布的内容,最简单的解决方案是将<fragment>
元素替换为FragmentContainerView
,然后将代码保留在您的活动中或在xml 中声明该类。请参阅the documentation 了解有关如何以两种方式执行此操作的示例。
【讨论】:
【参考方案2】:您的<fragment>
元素没有说明它应该加载哪个片段。您需要在<fragment>
中添加一个android:name
,其中android:name
的值是片段的完全限定类名:
<fragment
android:id="@+id/dayfragment"
android:name="com.example.dietmanagement.DayActivity"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</fragment>
【讨论】:
以上是关于当我切换到包含片段的活动时应用程序崩溃(二进制 XML 文件第 10 行:二进制 XML 文件第 10 行:膨胀类片段时出错)的主要内容,如果未能解决你的问题,请参考以下文章
ActivityNotFoundException,启动活动时出错