2012-10-08 71 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