在实施Firebase身份验证时在交换机案例中遇到无法访问的语句

时间:2017-04-01 12:11:12

标签: java android firebase switch-statement

"在切换案例下无法使用菜单项中的按钮注销语句。 "

 @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);
                    }
    }

enter image description here

3 个答案:

答案 0 :(得分:0)

在方法结束时写下这一行 "返回super.onOptionsItemSelected(item);"

答案 1 :(得分:0)

删除方法onOptionsItemSelected

中的第一行
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item); // Remove this line

    switch(item.getItemId()){

    }
}

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

答案 2 :(得分:0)

您可以使用return super.onOptionsItemSelected(item);或完全正确;结束开关盒后,不要忘记休息;声明。

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;