Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .finish()在嵌套的if语句中不起作用_Java_Android_Android Studio - Fatal编程技术网

Java .finish()在嵌套的if语句中不起作用

Java .finish()在嵌套的if语句中不起作用,java,android,android-studio,Java,Android,Android Studio,由于某些原因,我无法使用.finish()在嵌套的if语句中结束我以前的活动 if (id == R.id.nav_menu) { MenuFragment menuFragment = MenuFragment.newInstance("", ""); FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().replace(

由于某些原因,我无法使用.finish()在嵌套的if语句中结束我以前的活动

if (id == R.id.nav_menu) {
        MenuFragment menuFragment = MenuFragment.newInstance("", "");
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(
                R.id.layout_for_fragment,
                menuFragment
        ).commit();
    } else if (id == R.id.nav_report) {
        if(status == 1){
            SharedPrefManager.getInstance(getApplicationContext()).logout();
            getApplicationContext().finish();
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        }
    }

getApplicationContext().finish()它是上下文。使用
ClassNameYourActivity.this.finish()

。如果此代码在活动中,只需调用
this.finish()
,如果您使用getApplicationContext()创建新的意图,则需要将flag设置为->Intent.flag\u Activity\u new\u task只要您在活动主类中,只需调用finish并在内部类中调用MyActivity.this.finish()!