0

我有這種奇怪的方案,其中在onOrientationChange之後,我得到一個空指針錯誤,我的DrawerLayout在更改前已在onCreateView上初始化。DrawerLayout上的方向更改時的空指針

mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout); 

而且我在這一點上越來越空,

if(mDrawerList.getChildAt(position)!=null){ 
} 


private void selectItem(int position, boolean InstanceStateNull) { 
     Log.d("selectItem", "inside"); 

     //savePosition("position", position); 
      // update the main content by replacing fragments 
     Toast.makeText(getActivity(), "SELECT ITEM: "+ position, Toast.LENGTH_SHORT).show(); 
     Log.d("SELECT ITEM: Position: ", ""+ position); 

     this.mDrawerList.setItemChecked(position, true); 
     this.mDrawerList.setSelection(position); 

     String selectedPid = null; 
     //use mDrawerList.getChildAt(position) for not having null pointer 
     if(mDrawerList.getChildAt(position)!=null) 
     { 
      //Extracts the string 
      TextView pid = (TextView)mDrawerList.getChildAt(position).findViewWithTag("pid"); 
      selectedPid = pid.getText().toString(); 
      Log.d("SELECT ITEM: SELECTED ITEM: ", ""+ selectedPid.toString()); 
      Toast.makeText(getActivity(), "SELECTED ITEM: "+ selectedPid 
        , Toast.LENGTH_SHORT).show(); 
      //saveSelecteditem("selectedItem", selectedPid); 

     } 
     else{ 
      Log.i("DrawerList on Orientation Change: ", "Null"); 
      //Log.i("DrawerList on Orientation Change: ", "Count" + mDrawerList.getAdapter().getCount()); 

     } 
      } 

更新:

我一概不使用configChanges,我處理的方向變化手動。由於

+0

我猜,你會得到你的onOrientationChange之後,'position'將不再填寫。 – ThaMe90

+0

更改方向後,您的活動將重新創建,並且該活動中的所有變量都將重新初始化。所以'位置'不再被填滿。 – ThaMe90

+0

對於'layout-land'和'layout',你有一個特定的佈局文件'drawer_layout'嗎? – Geros

回答

0

試試這個在您的清單......

<activity android:name="Your Activity Name" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/> 
+0

Soory我忘了提及我沒有使用'configChanges',所以我正在手動處理它。謝謝 – rahstame