简体   繁体   中英

Null Pointer during orientation change on DrawerLayout

I have this weird scenario wherein after the onOrientationChange I got a Null Pointer Error, my DrawerLayout that has been initialized on onCreateView before the change.

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

And I am getting null on this point,

 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());

         }
           }

Update:

I am not and will not be using the configChanges , I am handling the orientation change manually. Thanks

Try this in your manifest...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM