1

我遇到了片段问题,我收到一条错误消息:

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:

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 导入到我的两个文件中并在互联网上查看,但没有一个解决方案有效。

4

2 回答 2

0

您的 activity_wizardsetup.xml 应如下所示:

<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 应该看起来像这样:

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

我相信应该修复它......在我的代码中,我使用FragmentPagerAdapter而不是 FragmentTransaction 或 FragmentManager ......但我认为修复 xml 会起作用......

于 2012-10-08T18:28:28.260 回答
0

我知道这是一个旧线程,但我想我还是会回答这个问题,因为这里的解决方案对我不起作用,但问题是一样的。

Exception was on inflating the fragment, because name was null. The fragment must have the following field:

android:name="com.google.android.gms.maps.MapFragment"

Source here.

于 2015-05-08T07:44:20.273 回答