gpt4 book ai didi

java - Android - 如何声明 'this' 的变量?

转载 作者:行者123 更新时间:2023-12-02 04:01:36 25 4
gpt4 key购买 nike

public class MainActivity extends AppCompatActivity {

//the problem is here
private class this_Variable=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


}

public void quitGame(View v){
this_Variable.finishAffinity();
}

}

正如您所看到的上面的代码,我想将“this”分配给一个名为“this_variable”的变量。

我尝试了上面的代码,但它不起作用。

最佳答案

刚刚解决了新手问题...用 MainActivity 替换类。

public class MainActivity extends AppCompatActivity {

//the problem is here
private MainActivity this_Variable=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


}

public void quitGame(View v){
this_Variable.finishAffinity();
}

}

或者只是

public void quitGame(View v){
MainActivity.this.finishAffinity();
}

关于java - Android - 如何声明 'this' 的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34836405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com