0

我有一個使用2個不同的佈局一個FragmentActivity取決於屏幕尺寸:片段Viewpager不同的屏幕尺寸

  1. XLARGE景觀。佈局有3個LinearLayouts,每個包含1個片段。

  2. 剩餘的屏幕尺寸。該佈局有1 ViewPager其中包含所有片段。

當屏幕方向在10英寸屏幕變化時,應用程序崩潰。它在橫向模式下使用屏幕尺寸1,在縱向模式下使用尺寸2。

的代碼:

public class RegistroAccionesPartidoActivity extends FragmentActivity { 


private static ArrayAdapter<Fragment> mFragmentArray; 
private ViewPager mMyPager; 
private Fragment1 mFragment1; 

private Fragment2 mFragment2; 
private Fragment3 mFragment3; 
private MyFragmentAdapter mFragmentsAdapter; 
private FragmentManager mFragmentManager; 
private FragmentTransaction mFragmentTrasaction; 


@Override 
public void onCreate(Bundle savedState) { 
    super.onCreate(savedState); 
    setContentView(R.layout.my_layout); 


    LinearLayout layoutFragment1 = (LinearLayout)findViewById(R.id.fragment1); 


    if(layoutFragment1 == null) { 

     mFragmentManager = getSupportFragmentManager(); 

     mMyPager = (ViewPager) findViewById(R.id.my_pager); 
     mFragmentArray = new ArrayAdapter<Fragment>(getApplicationContext(),android.R.layout.simple_list_item_1); 
     mFragmentsAdapter = new Fragment2Adapter(mFragmentManager); 


     if(savedState == null) { 

      mFragment2 = new Fragment2(true); 
      mFragment3 = new Fragment3(); 
      mFragment1 = new Fragment1(mIdPartido, mIdEquipoLocal, mIdEquipoVisitante); 
     } else { 

      mFragment1 = (Fragment1) getSupportFragmentManager().getFragment(savedState, "fragment1"); 
      mFragment2 = (Fragment2) getSupportFragmentManager(). 
      getFragment(savedState,"fragment2"); 
      mFragment3 = (Fragment3) getSupportFragmentManager(). 
      getFragment(savedState, "fragment3"); 
     } 


     mFragmentArray.add(mFragment1); 
     mFragmentArray.add(mFragment2); 
     mFragmentArray.add(mFragment3); 

     mDatosPartidoPager.setAdapter(mFragmentsAdapter); 

     mDatosPartidoPager.setCurrentItem(1); 


     } else { // xLarge Version 

     mFragmentManager = getSupportFragmentManager(); 
     mFragmentTrasaction = mFragmentManager.beginTransaction(); 


     if(savedState == null) { 
      mFragment1 = new Fragment1(); 
      mFragment2 = new Fragment2(); 
      mFragment3 = new Fragment3(); 


     } else { 
      mFragment1 = (Fragment1) getSupportFragmentManager(). 
       getFragment(savedState, "fragment1"); 

      mFragment2 = (Fragment2) getSupportFragmentManager(). 
       getFragment(savedState,"fragment2"); 
      mFragment3 = (Fragment3) getSupportFragmentManager(). 
       getFragment(savedState, "fragment3"); 
    } 
    mFragmentTrasaction.add(R.id.fragment1, mFragment1); 
    mFragmentTrasaction.add(R.id.fragment2, mFragment2); 
    mFragmentTrasaction.add(R.id.fragment3, mFragment3); 
    mFragmentTrasaction.commit(); 
    } 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    getSupportFragmentManager().putFragment(outState, "fragment2" , 
     mFragment2); 
    getSupportFragmentManager().putFragment(outState, "fragment3", 
     mFragment3); 

    getSupportFragmentManager().putFragment(outState,"fragment1", 
     mFragment1); 

    } 
} 

XLARGE佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

</LinearLayout> 


<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment3" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

</LinearLayout> 


</LinearLayout> 

其餘屏幕尺寸佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/my_pager"> 

    </android.support.v4.view.ViewPager> 

</LinearLayout> 

我具有根據屏幕改變的順序2不同勢崩潰:

  1. 開始景觀,該錯誤信息是:

java.lang.IllegalArgumentException異常:未找到ID爲0x觀點......對於Fragment2

  1. 當我開始縱向時,錯誤消息是:

java.lang.IllegalStateException:片段已經補充說:片段1

很抱歉的大問題。我只是試圖解釋我的問題。

非常感謝。

編輯:在大設備(​​如10「)橫向模式使用xLarge佈局和肖像模式使用其他佈局。

回答

0

對我來說(在XLarge Layout中),嵌套的LinearLayout-Elements混合水平和垂直方向看起來很奇怪。如果你以這種方式嵌套,請仔細觀察。

Greetings, Wolfgang

+0

感謝您的回答。當我改編我的代碼以獲得更好的解釋時,我犯了一個錯誤。它不在原始代碼中。 – gutiory 2012-02-09 08:53:04