2015-05-28 22 views
0

我第一次檢查片段,我創建了一個帶有FrameLayout的LinearLayout,併爲我的layout-sw600dp創建了一個帶有2個FrameLayout的LinearLayout。FragmentManager給我NPE

其中一個標識爲「main」,另一個標識爲「details」。

我得到的錯誤是:

Caused by: java.lang.NullPointerException 
     at android.app.BackStackRecord.doAddOp(BackStackRecord.java:395) 
     at android.app.BackStackRecord.add(BackStackRecord.java:385) 
     at mes.fallstudio.tvfall.MainActivity.onCreate(MainActivity.java:28) 

,其中第28行是在這裏的最後一行:

public class MainActivity extends AppCompatActivity { 
private android.support.v4.app.Fragment mainFragment; 
private android.app.Fragment detailsFragment; 
private Boolean isDualPane = false; 

private android.support.v4.app.FragmentManager fm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
    mainFragment = fragmentManager.findFragmentById(R.id.main); 

    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.main, mainFragment).commit(); 


} 
} 

XML佈局文件:

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


<FrameLayout 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

<FrameLayout 
    android:id="@+id/details" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

這是爲layout-sw600dp和對於佈局文件夾的xml文件,沒有第二個FrameLayout(細節)。

如果有幫助,我將擴展AppCompatActivity。 謝謝

+0

你可以添加你的XML佈局文件? –

+0

@SvenDubbeld剛剛做到了。 – vicolored

+0

我真的不明白爲什麼要添加一個已經存在的片段,或者也沒有 – njzk2

回答

3

AppCompatActivity使用getSupportFragmentManager()而不是getFragmentManager()

參考:https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html

編輯:

在你的代碼試圖從XML佈局得到mainFragment。但是,你不在那裏定義它。你可以在你的代碼實例mainFragment

mainFragment = new MainFragment(); 

或者在你的XML定義它:

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

<fragment android:name="mes.fallstudio.tvfall.MainFragment" 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<FrameLayout 
    android:id="@+id/details" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

在後一種情況下,你不需要使用FragmentManager都在你的代碼,除非你需要在運行時操作碎片。

在這兩種情況下,你還需要實際的片段:

public class MainFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.main_fragment, container, false); 
    } 
} 
+0

仍在獲得NPE。我已更新代碼 – vicolored

+0

請現在嘗試 –

+0

是的,這是它。謝謝!! – vicolored