我试图在我的主要活动中加载一个片段,该片段稍后将成为我的导航标签之一。问题是应用程序崩溃并说该片段无法膨胀。 我发现了其他类似的问题,但没有一个解决方案可以帮助我
这是我的代码
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_about布局替换片段。我对吗?
请帮助我!谢谢!
答案 0 :(得分:1)
onCreateView()
FragmentAbout
小写'o'
将来,请尝试使用java注释@Override
来避免这个愚蠢的错误