【问题标题】:Android Fragment in Fragment inflate errorAndroid Fragment in Fragment inflate 错误
【发布时间】:2020-03-20 10:28:26
【问题描述】:

我是 Android 开发新手。另一个Fragment中的Fragment布局无法显示的Fragment问题。我用了将近一周的时间寻求解决方案...

如果我从 MainFragment.java 膨胀“fragment_main2”,它就无法运行。但是如果我膨胀“fragment_checker.xml”,那就没问题了。任何建议,非常感谢!

结构如下图:

MainActivity.java(创建并显示 MainFragment)

package a3.webchecker;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {
public StringBuilder log = new StringBuilder("Web Checker Log:\n");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main2);
        Fragment fragment = new MainFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container, fragment)
                .addToBackStack(null)
                .commit();
    }
}

MainFragment.java(膨胀 fragment_main2.xml)

package a3.webchecker;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class MainFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main2, container, false);
        return view;
    }
}

activity_main.xml(包含 FrameLayout 容器)

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="8dp"
    tools:context=".MainActivity">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

fragment_main2.xml(包含 Fragment 选项卡和布局正在使用 fragmentChecker.xml)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainFragment">
    <fragment
        android:id="@+id/web_checker_1"
        android:name="a3.webchecker.CheckerFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        tools:layout="@layout/fragment_checker" />
    <Button
        android:id="@+id/view_log"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="View Log" />
</LinearLayout>

fragment_checker.xml(布局)

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
    app:cardUseCompatPadding="true"
    app:contentPadding="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        tools:context=".CheckerFragment">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="URL:"
            app:layout_constraintBaseline_toBaselineOf="@+id/url"
            app:layout_constraintStart_toStartOf="parent" />

        <EditText
            android:id="@+id/url"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toEndOf="@+id/textView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status: "
            app:layout_constraintBaseline_toBaselineOf="@+id/status"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/status"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/url" />

        <Button
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Check"
            app:layout_constraintEnd_toStartOf="@+id/go_to"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/status" />

        <Button
            android:id="@+id/go_to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go to"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/check"
            app:layout_constraintTop_toTopOf="@+id/check" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    在“fragment_main2.xml”中,您没有正确在 tools:context

    中设置活动

    修复成这个

    tools:context=".MainActivity"
    

    而且因为你实现了片段事务,你必须在你的活动中定义布局片段。

    <LinearLayout 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"
        android:padding="8dp"
        tools:context=".MainActivity">
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="[your fragment location]"
            android:id="@+id/fragment_container" />
    </LinearLayout>
    

    【讨论】:

    • 感谢 Kelvin,刚刚被您的评论更改了。并收到错误消息 "java.lang.StackOverflowError: stack size 8MB" ,正在关闭 VM。我还在努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多