繁体   English   中英

实现 Firebase Auth 时在 switch case 中遇到 Unreachable 语句

[英]Encountering Unreachable statement in switch case while implementing Firebase Auth

“在使用菜单项中的按钮退出时,无法在 switch case 下访问语句。”

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.sign_out_menu:

                AuthUI.getInstance().signOut(this); //NOT REACHABLE
                return true;

                default:
                    return super.onOptionsItemSelected(item);
                    }
    }

在此处输入图片说明

在方法“return super.onOptionsItemSelected(item);”的末尾写下这一行

去掉onOptionsItemSelected方法中的第一行

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item); // Remove this line

    switch(item.getItemId()){

    }
}

因为您的代码中有一个 return 语句,所以无法访问第一个return语句下不属于switch内容

您可以使用 return super.onOptionsItemSelected(item); 或者只是真的; 结束 switch case 后,不要忘记 break; 声明也是。

int id = item.getItemId();
    switch (id) {
        case R.id.id1:
            Fragment1 fragment1 = new Fragment1();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(((ViewGroup)getView().getParent()).getId()  , fragment1);
            fragmentTransaction.commit();
            break;
        case R.id.id2:
            Fragment2 fragment2 = new Fragment2();
            FragmentManager fragmentManager1 = getFragmentManager();
            FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
            fragmentTransaction1.replace(((ViewGroup)getView().getParent()).getId()  , fragment2);
            fragmentTransaction1.commit();
            break;            

    }

    return true;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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