片段错误充气类片段 [英] Fragment Error inflating class fragment

查看:156
本文介绍了片段错误充气类片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦片段我得到了一个错误,指出:

I have some trouble with fragment I got an error that says:

Binary XML file line #2 Error: inflating class fragment.
Cause by: Java.lan.NullPointerException: name == null

这是我的二进制文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:id="@+id/fragment_content"
        android:name="com.livetrekker.fragments.WelcomeFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

然后我FragmentActivity:

Then my FragmentActivity:

public class SetupWizard extends FragmentActivity{
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.activity_wizardsetup);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        WelcomeFragment wf = new WelcomeFragment();
        fragmentTransaction.add(R.id.fragment_content, wf);

        fragmentTransaction.commit();
    }
}

和我的最后片段:

Public class WelcomeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.welcomelivetrekker,container, false);
    }
}

我已导入Android.support.v4.app.Fragment到我的两个文件,​​并检查了在互联网上,但没有解决方案的工作。

I have imported the Android.support.v4.app.Fragment to both of my file and check out on the internet, but none of the solution is working.

推荐答案

您应该activity_wizardsetup.xml看起来是这样的:

Your activity_wizardsetup.xml should look something like this:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
</android.support.v4.view.ViewPager>

那么你fragment_content.xml看起来应该以这样的效果:

Then your fragment_content.xml should look something to this effect:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

</LinearLayout>

我认为,应该修复它......在我的codeI使用的 FragmentPagerAdapter 而非FragmentTransaction或FragmentManager ...但我认为固定XML起来将工作...

I believe that should fix it... in my code i use a FragmentPagerAdapter rather than FragmentTransaction or FragmentManager... but i think fixing the xml up will work...

这篇关于片段错误充气类片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆