引起:android.view.InflateException:二进制XML文件行#13:错误膨胀类片段

问题描述:

我想通过代码添加片段到活动之间交换,但我不断收到'引起:android.view。 InflateException:二进制XML文件行#13:错误充气类片段”引起:android.view.InflateException:二进制XML文件行#13:错误膨胀类片段

下面是主要活动的代码:

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文件:

<?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> 

对于fragme NT

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文件:

<?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> 
+1

全logcat的,请 –

阅读你的代码...

//添加片段到 'fragment_container' 的FrameLayout

你可能需要改变

<fragment 
    android:id="@+id/fragment_container" 

<FrameLayout 
    android:id="@+id/fragment_container" 

或者,不使用Java代码来创建片段和简单的类属性添加到XML片段块。从文档中...

<fragment>中的android:name属性指定要在布局中实例化的Fragment类。

否则,您可以显示片段,像这样

Fragment wel = new WelcomeFragment(); // correct naming schema 
wel.setArguments(getIntent().getExtras()); 
getSupportFragmentManager() 
    .beginTransaction() 
    .add(R.id.fragment_container, wel) 
    .commit(); 
+0

1.我婉后换的片段,我阅读我需要通过Java添加它。 2.现在我没有发现任何异常,但我也没有看到屏幕上的任何内容,只是一个空白的活动。 – Omar

+0

它是一个或另一个。使用'',不需要Java来显示片段。如果您想完全控制FragmentTransaction,您可以使用FrameLayout(或其他ViewGroup)。您需要在添加方法 –

+0

后提交FragmentTransaction我是个白痴,但是谢谢-_- – Omar

声明你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> 
+0

谢谢,现在我没有发现任何异常,但我也没有看到屏幕上的任何内容,只是一个空白活动 – Omar

+0

您需要调用'ft.commit();'在onCreate的最后 –