Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Android-我的全局变量值在切换应用程序后返回null_Android_Global Variables - Fatal编程技术网

Android-我的全局变量值在切换应用程序后返回null

Android-我的全局变量值在切换应用程序后返回null,android,global-variables,Android,Global Variables,我使用GlobalVariable文件在整个应用程序中保存数据,但如果我切换到另一个应用程序并返回,它将返回null值 下面是我的代码: 在舱单中: <application .. android:name=".MyApplication" > 活动内部 int mRowId = ((MyApplication) getApplicationContext()).rowId; 您需要将其设置为静态和最终,但只能与getter和setter一起使用 类似于下面的内

我使用GlobalVariable文件在整个应用程序中保存数据,但如果我切换到另一个应用程序并返回,它将返回null值

下面是我的代码:

在舱单中:

 <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并使其保持公共静态即可。我以为你的想法只是保持变量不变,但要记住,这会改变的。所以别忘了跟踪你所有的变化。