2011-12-05 42 views
0

我有以下代碼:FindFragmentById不返回空

// Compruebo SI TENGO洛杉磯DOS fragmentos

FichaAlumnoFragment frag=(FichaAlumnoFragment)getSupportFragmentManager().findFragmentById(R.id.details_students_frag); 
     if (frag!=null) { 
      Log.i(this.getClass().getSimpleName(),"Tengo dos fragmentos"); 
      mDualFragments=true; 
     } 

如果我在縱向模式是它返回null。如果我更改爲landscape(模擬器中的Ctrl + 11),則frag有一個值並且佈局正確呈現,但如果我再次更改爲縱向,則應該有一個空值,但會返回一些值並且應用程序崩潰。

佈局是不同的兩個方向

想我誤解片段lifecylce。任何想法?

回答

1

我有同樣的問題。您仍然必須檢查爲空,但是還要檢查方法isInLayout(),例如:

if (null == getSupportFragmentManager().findFragmentById(R.id.fragment_example) || !getSupportFragmentManager().findFragmentById(R.id.fragment_example).isInLayout()) { 
    // Fragment has not been created or it is not visible 
} 
相關問題