[英]Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
I am trying to add fragments to the activity through code to swap between them but I keep getting 'Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment' 我试图通过代码将片段添加到活动中以在它们之间交换,但我不断收到“原因:android.view.InflateException:二进制XML文件行#13:错误夸大类片段”
Here is the code for the main activity: 这是主要活动的代码:
package com.example.user.timetable_test;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class Set_Up extends FragmentActivity{
//MiscData data = MiscData.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set__up);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment wel = new frag_welcome();
wel.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
ft.add(R.id.fragment_container, wel);
}
}
XML file: XML档案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_set_up"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.timetable_test.Set_Up">
<fragment
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
For the fragment 对于片段
package com.example.user.timetable_test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class frag_welcome extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.frag_set_up_welcome, container, false);
return view;
}
}
XML file: 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:text="@string/welcome_to_the_timetable_app"
android:layout_width="150dp"
android:layout_height="wrap_content" android:id="@+id/textView2"
android:layout_marginTop="100dp"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:text="@string/start_set_up_butt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="69dp"
android:id="@+id/startSetUp" style="@style/Widget.AppCompat.Button.Borderless"
android:layout_below="@+id/textView3" android:layout_centerHorizontal="true"/>
<TextView
android:text="@string/press_start_set_up_to_start"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textAlignment="center"
android:layout_marginTop="24dp"
android:layout_width="170dp" android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Reading your code... 读取您的代码...
// Add the fragment to the 'fragment_container' FrameLayout
//将片段添加到“ fragment_container” FrameLayout
You probably need to change 您可能需要更改
<fragment
android:id="@+id/fragment_container"
To 至
<FrameLayout
android:id="@+id/fragment_container"
Or, not use the Java code to create the Fragment and simply add the class attribute to the XML fragment block. 或者,不要使用Java代码创建Fragment,而只需将class属性添加到XMLfragment块中。 From the documentation...
从文档中...
The
android:name
attribute in the<fragment>
specifies the Fragment class to instantiate in the layout.<fragment>
的android:name
属性指定要在布局中实例化的Fragment类。
Otherwise, you can show the Fragment like so 否则,您可以像这样显示片段
Fragment wel = new WelcomeFragment(); // correct naming schema
wel.setArguments(getIntent().getExtras());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.fragment_container, wel)
.commit();
Declare your fragment_container like this, 这样声明您的fragment_container,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_set_up"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.timetable_test.Set_Up">
<!-- Can be any child of ViewGroup: RelativeLayout/LinearLayout/... -->
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.