gpt4 book ai didi

java - Android 上的 Fragment 膨胀问题

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

我正在尝试在我的主 Activity 中加载一个 fragment ,该 fragment 稍后将成为我的导航选项卡之一。问题是应用程序崩溃并说 fragment 无法膨胀。我发现了其他类似的问题,但没有一个解决方案可以帮助我

这是我的代码

MainActivity.java

public class MainActivity extends FragmentActivity {


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

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.projecttabs.MainActivity">

<fragment
android:id="@+id/fragment1"
android:name="com.example.projecttabs.FragmentAbout"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />

</LinearLayout>

FrgagmentAbout.java

public class FragmentAbout extends Fragment {

public View OnCreateView(LayoutInflater l, ViewGroup v, Bundle savedInstanceState){

return l.inflate(R.layout.fragment_about,v,false);
}

}

fragment_about.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"
android:orientation="vertical"
android:background="#000">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="182dp"
android:text="ButtonTest" />

</RelativeLayout>

logCat

FATAL EXCEPTION: main
Process: com.example.projecttabs, PID: 1709
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projecttabs/com.example.projecttabs.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1928)
at com.example.projecttabs.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.example.projecttabs.FragmentAbout did not create a view.
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)

据我了解,MainActivity 类将 contentView 设置为 main_activity 布局,然后,在 main_activity.xml 文件中,调用 FragmentAbout 类以将 fragment 替换为 fragment_about 布局。我对吗?

请帮帮我!

谢谢!

最佳答案

FragmentAbout中应该是onCreateView()加上小写的'o'

以后尝试使用java注解@Override来避免这种愚蠢的bug

关于java - Android 上的 Fragment 膨胀问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24100168/

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