Android-我的全局变量值在切换应用程序后返回null
我使用GlobalVariable文件在整个应用程序中保存数据,但如果我切换到另一个应用程序并返回,它将返回null值 下面是我的代码: 在舱单中:Android-我的全局变量值在切换应用程序后返回null,android,global-variables,Android,Global Variables,我使用GlobalVariable文件在整个应用程序中保存数据,但如果我切换到另一个应用程序并返回,它将返回null值 下面是我的代码: 在舱单中: <application .. android:name=".MyApplication" > 活动内部 int mRowId = ((MyApplication) getApplicationContext()).rowId; 您需要将其设置为静态和最终,但只能与getter和setter一起使用 类似于下面的内
<application
..
android:name=".MyApplication" >
活动内部
int mRowId = ((MyApplication) getApplicationContext()).rowId;
您需要将其设置为
静态
和最终
,但只能与getter和setter一起使用
类似于下面的内容
public class MyApplication extends Application {
public final static int rowId = 0;
请不要忘记您的初始化、设置和获取程序:
int rowId;
public int getRowId() {
return rowId;
}
public void setRowId(int rowId) {
this.rowId = rowId;
}
要从类外部设置值,请执行以下操作:
MyApplication.rowId = //whatever returns int
int TempInt = MyApplication.rowId; // TempInt will have the value of rowId
要从类外部/内部获取值,请执行以下操作:
MyApplication.rowId = //whatever returns int
int TempInt = MyApplication.rowId; // TempInt will have the value of rowId
检查什么是setter和getter:
将rowId设置为static尝试此
公共最终静态int rowId=0
但初始化后不能将值设置为final变量。如果要在初始化后更改它,只需删除final并使其保持公共静态即可。我以为你的想法只是保持变量不变,但要记住,这会改变的。所以别忘了跟踪你所有的变化。