我在侧onConfigurationChanged
函数中声明了一个int jj
,现在我想在同一个类中的onConfigurationChanged
任何地方访问它。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
final int jj=4;
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
final int jj=2;
}
}
我想在同一个类中访问这个变量,如下所示:
public class MainActivity extends Activity {
public static int aa=jj;
}
最佳答案
将其声明为类字段。我不知道你在用 aa
做什么。
public class MainActivity extends Activity {
public int jj;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
jj=4;
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
jj=2;
}
}
}
从问题中可以看出,你并没有真正理解编程、OOP和Java的基础知识。
我建议您先完成一些基本教程,然后再继续,直到您了解基元与对象、final
和 static
。
您还应该了解不能将 Activity
视为 POJO。
关于java - 在同一类中的方法之外的任何地方访问整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28261939/