2017-04-18 65 views
0

错误充气类片段错误android.view.InflateException:二进制XML文件行#21:二进制XML文件行#21:错误充气类片段


我试图在活动添加片段动态运行。
运行应用程序崩溃后。
文件名是
有一个主活性(MainActivity.java),它的XML(activity_main.xml中)
片段(MainFragment.java),它的XML(fragment_main.xml)

以下是应用程序的代码

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_main" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="119dp" 
     android:layout_marginStart="119dp" 
     android:layout_marginTop="63dp" 
     android:text="Button" /> 
</RelativeLayout> 

MainFragment.java

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MainFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_main, container, false); 
     return view; 
    } 
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.vikashyadav.myquiz.MainActivity"> 

    <fragment 
     android:id="@+id/fragment_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     /> 


</RelativeLayout> 

MainActivity.java

package com.example.vikashyadav.myquiz; 

import android.os.Bundle; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); 
     ft.add(R.id.fragment_holder,new MainFragment()); 
     ft.commit(); 
    } 


} 

我无法找到为什么这崩溃。其他类似的FrameLayout

+0

把所有的堆栈跟踪这里。 –

+0

不应该片段有一个名称或类属性? – Selvin

回答

1

删除片段布局..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.vikashyadav.myquiz.MainActivity"> 

<FrameLayout 
    android:id="@+id/fragment_holder" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    /> 

+0

感谢它为我工作。 –

0
import android.os.Bundle; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     addFragment(R.id.fragment_holder,new MainFragment(), "MainFragment"); 
    } 

    protected void addFragment(@IdRes int containerViewId, @NonNull Fragment fragment,@NonNull String fragmentTag) { 
     getSupportFragmentManager() 
       .beginTransaction() 
       .add(containerViewId, fragment, fragmentTag) 
       .disallowAddToBackStack() 
       .commit(); 
    } 

} 
相关问题