App crashing after i rotate device and click the menu

☆樱花仙子☆ 提交于 2019-12-13 07:15:40

问题


I have a navigation drawer with fragments and my app is crashing after I rotate the device and click on the menu. My code on main activity is:

public class MainActivity extends FragmentActivity {


    private DrawerLayout mDrawerLayout;
    ImageView home;
    Fragment fragment = null;
    TextView appname;
    ExpandableListView expListView;
    HashMap<String, List<String>> listDataChild;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fontPath = "neue.ttf";
        setContentView(R.layout.activity_main);
        home = (ImageView)findViewById(R.id.home);
        home.setOnClickListener(homeOnclickListener);
        appname = (TextView)findViewById(R.id.appname);
        Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
        appname.setTypeface(tf);

        if(savedInstanceState == null){
            setUpDrawer();
        }
    }


    private void setUpDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
        mDrawerLayout.setDrawerListener(mDrawerListener);
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        prepareListData();
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        // setting list adapter
        expListView.setAdapter(listAdapter);
        fragment = new Inicio();
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
        mDrawerLayout.closeDrawer(expListView);

        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                switch (groupPosition) {
                    case 0:
                        switch (childPosition) {
                            case 0:
                                fragment = new enunciados_definiciones();
                                break;
                            case 1:
                                fragment = new enunciados_ayuda_victima();
                                break;
                            case 2:
                                fragment = new enunciados_abordaje();
                                break;
                            case 3:
                                fragment = new transmision_enfermedades();
                                break;
                            default:
                                fragment = new Inicio();
                                break;
                        }
                        break;

                    case 1:
                        switch (childPosition) {
                            case 0:
                                fragment = new Inicio();
                                break;
                            case 1:
                                fragment = new Inicio();
                                break;
                            case 2:
                                fragment = new Inicio();
                                break;
                            default:
                                break;
                        }
                        break;

                    case 2:
                        switch (childPosition) {
                            case 0:
                                fragment = new Inicio();
                                break;
                            case 1:
                                fragment = new Inicio();
                                break;
                            case 2:
                                fragment = new Inicio();
                                break;
                            default:
                                break;
                        }
                        break;

                    default:
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
                mDrawerLayout.closeDrawer(expListView);
                return false;
            }
        });
    }

My code continues like this logcat error (line: 140) its on the next piece of code:

View.OnClickListener homeOnclickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mDrawerLayout.isDrawerOpen(expListView)){
                mDrawerLayout.closeDrawer(expListView);
            }else{
                mDrawerLayout.openDrawer(expListView);
            }
        }
    };

My logcat:

    FATAL EXCEPTION: main
java.lang.NullPointerException
at com.eiusweb.primerosauxilios.MainActivity$2.onClick(MainActivity.java:140)
at android.view.View.performClick(View.java:4162)
at android.view.View$PerformClick.run(View.java:17082)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)

Thanks


回答1:


You only call setupDrawer if your savedInstanceState is null. This mean it isn't called when you recreate the activity on rotation. However that's where you set the value of mDrawerLayout . So when onClick is called after rotation, mDrawerLayout is null. To fix this, you need to set the value of mDrawerLayout in both cases.

Actually I have no idea why you aren't calling setupDrawer when savedInstanceState is non null, it seems very wrong.




回答2:


Move

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
expListView = (ExpandableListView) findViewById(R.id.lvExp);

from setUpDrawer() to onCreate() before if block.




回答3:


Ok i got it, had to modify somethings but it worked. Took out the setUpDrawer() from the if null, like suggest from Gabe Sechan. and change it to

setUpDrawer();
        if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Inicio()).commit();
        }

I also deleted fragment = new Inicio(); and all the getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

And updated all cases to case 0:fragment = new enunciados_definiciones(); break; Thanks



来源:https://stackoverflow.com/questions/36900671/app-crashing-after-i-rotate-device-and-click-the-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!