2012-10-08 70 views
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导入到我的文件中,并在互联网上查看,但没有一个解决方案正在运行。

回答

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将起作用...

+0

它的工作表示感谢。只是一个小错误,它是 Kalimero95

0

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

由于名称为空,因此例外是对该片段进行膨胀。 片段必须具备以下领域:

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

来源here