gpt4 book ai didi

java - 当我切换到包含 fragment 的 Activity 时应用程序崩溃(二进制 XML 文件行 #10 : Binary XML file line #10: Error inflating class fragment)

转载 作者:行者123 更新时间:2023-12-04 07:55:05 27 4
gpt4 key购买 nike

当我按下按钮打开另一个包含 fragment 的 Activity 时,我的应用程序崩溃。我是 Android Studio 的新手,似乎找不到任何解决方案。
在activity_main.xml 中,我删除了这个问题的其他按钮,因为我想主要关注这个monday_btn。
我研究了这种类型的错误并尝试了很多技巧,但似乎没有一个有用。
主 Activity .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);
}
});

}

}
日 Activity .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_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_green"
tools:context=".DayFragment">

<TextView
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/day" />

</FrameLayout>
Activity 日.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_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_green"
tools:context=".DayActivity">

<fragment
android:id="@+id/dayfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_green"
tools:context=".MainActivity">

<Button
android:id="@+id/monday_btn"
android:layout_width="140dp"
android:layout_height="86dp"
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 ComponentInfo{com.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)

最佳答案

您正在尝试以两种不同的方式加载 fragment - 直接通过带有 <fragment> 的布局标签,这要求您指定实际要加载的 Fragment,并作为 Activity 中的事务,需要一个布局来放置 Fragment。

// This says - "hey Android load up a fragment for me", but you don't say which
<fragment
android:id="@+id/dayfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
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 的元素并将代码留在您的 Activity 中或在 xml 中声明类。请查看 the documentation有关如何以两种方式执行此操作的示例。

关于java - 当我切换到包含 fragment 的 Activity 时应用程序崩溃(二进制 XML 文件行 #10 : Binary XML file line #10: Error inflating class fragment),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66737974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com