gpt4 book ai didi

安卓.view.InflateException : Error inflating fragment

转载 作者:行者123 更新时间:2023-11-30 02:40:16 25 4
gpt4 key购买 nike

在 MainActivity 中,我正在实例化一个像这样的简单 fragment :

Fragment fragment = new RentFragment();

RentFragment.java 是这样的:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RentFragment extends Fragment {

public RentFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_rent, container, false);

return rootView;
}

}

fragment_rent.xml 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Rent"
android:textSize="16dp" />

<fragment android:name="com.example.android.fragments.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</RelativeLayout>

错误日志是这样的:

09-16 13:32:32.759: E/AndroidRuntime(9131): FATAL EXCEPTION: main
09-16 13:32:32.759: E/AndroidRuntime(9131): android.view.InflateException: Binary XML file line #14: Error inflating class fragment
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-16 13:32:32.759: E/AndroidRuntime(9131): at com.example.devicerentalsystem.RentFragment.onCreateView(RentFragment.java:15)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.Fragment.performCreateView(Fragment.java:1695)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.BackStackRecord.run(BackStackRecord.java:682)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.os.Handler.handleCallback(Handler.java:730)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.os.Looper.loop(Looper.java:137)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-16 13:32:32.759: E/AndroidRuntime(9131): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 13:32:32.759: E/AndroidRuntime(9131): at java.lang.reflect.Method.invoke(Method.java:525)
09-16 13:32:32.759: E/AndroidRuntime(9131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-16 13:32:32.759: E/AndroidRuntime(9131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 13:32:32.759: E/AndroidRuntime(9131): at dalvik.system.NativeStart.main(Native Method)
09-16 13:32:32.759: E/AndroidRuntime(9131): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.fragments.HeadlinesFragment: make sure class name exists, is public, and has an empty constructor that is public
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.Fragment.instantiate(Fragment.java:592)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.Fragment.instantiate(Fragment.java:560)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.Activity.onCreateView(Activity.java:4738)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-16 13:32:32.759: E/AndroidRuntime(9131): ... 19 more
09-16 13:32:32.759: E/AndroidRuntime(9131): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.fragments.HeadlinesFragment" on path: DexPathList[[zip file "/data/app/com.example.devicerentalsystem-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.devicerentalsystem-1, /vendor/lib, /system/lib]]
09-16 13:32:32.759: E/AndroidRuntime(9131): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-16 13:32:32.759: E/AndroidRuntime(9131): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-16 13:32:32.759: E/AndroidRuntime(9131): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-16 13:32:32.759: E/AndroidRuntime(9131): at android.app.Fragment.instantiate(Fragment.java:582)
09-16 13:32:32.759: E/AndroidRuntime(9131): ... 22 more

我做错了什么?提前致谢。

最佳答案

我想我找到了问题所在。一个 fragment 不能容纳另一个 fragment 。所以我在 xml 中添加了一个框架布局,并使用 fragment 管理器将其替换为我自己的 fragment 。

感谢大家的帮助。

XML:

    <!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


<Button
android:id="@+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="scanAgain"
android:text="Re-Scan" />

</RelativeLayout>

Java 的变化:

View rootView = inflater.inflate(R.layout.fragment_add, container, false);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.sample, (Fragment)headlineFrag).commit();

关于安卓.view.InflateException : Error inflating fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25867739/

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