android-fragments 添加片段到xml崩溃我的应用程序-如何修复?

9njqaruj  于 2022-11-13  发布在  Android
关注(0)|答案(3)|浏览(152)

这是我第一次在android中处理碎片。
由于某种原因,我的应用程序崩溃时,我试图添加片段标记到我的主要活动。我知道有很多问题类似我的,但我找不到任何有用的这种情况。
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="com.group.peixa.test.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.group.peixa.test.BlankFragment"
    />

</android.support.constraint.ConstraintLayout>

fragment_blank.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.group.peixa.test.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

BlankFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends AppCompatDialogFragment {

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

}

MainActivity.java

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 }

崩溃日志

09-28 09:28:18.288 3931-3931/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.group.peixa.test, PID: 3931
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.group.peixa.test/com.group.peixa.test.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                              Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                 at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                                                 at android.app.Activity.setContentView(Activity.java:2166)
                                                 at com.group.peixa.test.MainActivity.onCreate(MainActivity.java:11)
                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                              Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                 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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                 at android.app.Activity.setContentView(Activity.java:2166) 
                                                 at com.group.peixa.test.MainActivity.onCreate(MainActivity.java:11) 
                                                 at android.app.Activity.performCreate(Activity.java:6237) 
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                              Caused by: java.lang.IllegalArgumentException: Binary XML file line #0: Must specify unique android:id, android:tag, or have a parent with an id for com.group.peixa.test.BlankFragment
                                                 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3645)
                                                 at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                 at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
                                                 at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
                                                 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                 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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                 at android.app.Activity.setContentView(Activity.java:2166) 
                                                 at com.group.peixa.test.MainActivity.onCreate(MainActivity.java:11) 
                                                 at android.app.Activity.performCreate(Activity.java:6237) 
                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:148) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
slsn1g29

slsn1g291#

您的xml将如下所示,只需添加

android:id="@+id/frag1"

你的< fragment >标签。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="com.group.peixa.test.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:id="@+id/frag1"
        android:layout_height="wrap_content"
        android:name="com.group.peixa.test.BlankFragment"
    />
</android.support.constraint.ConstraintLayout>

查看官方文档https://developer.android.com/guide/components/fragments.html
注意:每个片段都需要一个唯一得标识符,以便在活动重新启动时系统可以使用该标识符还原片段(并且您可以使用该标识符捕获片段以执行事务,如删除片段).有三种方法可以为片段提供ID:
为android:id属性提供一个唯一的ID。为android:tag属性提供一个唯一的字符串。如果您未提供前两个属性,系统将使用容器视图的ID。

lzfw57am

lzfw57am2#

将代码更改为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.group.peixa.test.MainActivity">

    <fragment
        android:id="@+id/frag_blank"
        android:name="com.group.peixa.test.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>
yws3nbqq

yws3nbqq3#

安卓30+

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.group.peixa.test.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/frag_blank"
        android:name="com.group.peixa.test.BlankFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

相关问题