Android 使用全局的静态变量,引发一系列诡异错误
首先需要重写Application,主要重写里面的onCreate方法,就是创建的时候,初始化变量的值。然后在整个应用中的各个文件中就可以对该变量进行操作了。
启动Application时,系统会创建一个PID,即进程ID,所有的Activity就会在此进程上运行。那么我们在Application创建的时候初始化全局变量,同一个应用的所有Activity都可以取到这些全局变量的值,换句话说,我们在某一个Activity中改变了这些全局变量的值,那么在同一个应用的其他Activity中值就会改变。下面举个例子详细介绍一下应用步骤。
下面是MyApp.java
package com.android.test;
import android.app.Application;
public class MyApp extends Application{
private String mylabel ;
public String getLabel(){
return mylabel;
}
public void setLabel(String s){
this.mylabel = s;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setLabel("Welcome!"); //初始化全局变量
}
}
下面是mainActivity.java
package com.ginwave.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class mainActivity extends Activity {
private MyApp myApp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp
Log.i("guoll", "InitLabel:"+myApp.getLabel()); //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值
myApp.setLabel("Changing!"); //修改一下
Log.i("guoll", "ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有
Intent intent = new Intent(); //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值
intent.setClass(this, otherActivity.class);
startActivity(intent);
}
}
另一个otherActivity.java:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class otherActivity extends Activity{
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp
Log.i("guoll", "OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了
}
}
修改配置文件ApplicationManifest.xml,将要运行的应用程序MyApp加进去:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<!-- 在这里,将默认的Application设置成自己做的MyApp-->
<application android:name="MyApp"
android:icon="@drawable/icon"
android:label="@string/app_name"
>
<activity android:name=".mainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".otherActivity">
</activity>
</application>
</manifest>
发布日期:2021-10-17 15:07:12
浏览次数:2
分类:技术文章
本文共 2795 字,大约阅读时间需要 9 分钟。
开发中,不知道你有没有发现,static非常好用,但是却暴露很大问题,一些莫名奇妙的错误,经常遇到的是:空指针等问题,static访问是无法跨进程的。Android中的Activity,Service是可以在各自进程中运行的,用static传递参数到不同进程的Activity、Service时会错。 static占据的那份内存,不知道就很可能被回收掉了,导致出错。
如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型;而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就是使用Application context。
首先需要重写Application,主要重写里面的onCreate方法,就是创建的时候,初始化变量的值。然后在整个应用中的各个文件中就可以对该变量进行操作了。
启动Application时,系统会创建一个PID,即进程ID,所有的Activity就会在此进程上运行。那么我们在Application创建的时候初始化全局变量,同一个应用的所有Activity都可以取到这些全局变量的值,换句话说,我们在某一个Activity中改变了这些全局变量的值,那么在同一个应用的其他Activity中值就会改变。下面举个例子详细介绍一下应用步骤。
下面是MyApp.java
package com.android.test;
import android.app.Application;
public class MyApp extends Application{
private String mylabel ;
public String getLabel(){
return mylabel;
}
public void setLabel(String s){
this.mylabel = s;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setLabel("Welcome!"); //初始化全局变量
}
}
下面是mainActivity.java
package com.ginwave.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class mainActivity extends Activity {
private MyApp myApp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp
Log.i("guoll", "InitLabel:"+myApp.getLabel()); //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值
myApp.setLabel("Changing!"); //修改一下
Log.i("guoll", "ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有
Intent intent = new Intent(); //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值
intent.setClass(this, otherActivity.class);
startActivity(intent);
}
}
另一个otherActivity.java:
package com.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class otherActivity extends Activity{
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp
Log.i("guoll", "OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了
}
}
修改配置文件ApplicationManifest.xml,将要运行的应用程序MyApp加进去:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<!-- 在这里,将默认的Application设置成自己做的MyApp-->
<application android:name="MyApp"
android:icon="@drawable/icon"
android:label="@string/app_name"
>
<activity android:name=".mainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".otherActivity">
</activity>
</application>
</manifest>
转载地址:https://blog.csdn.net/tenggangren/article/details/8971368 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年03月29日 07时03分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
mysql 自适应字段宽度_box-sizing解决自适应布局容器宽度问题
2019-04-21
java 配置文件配置路径_Java读取配置文件路径设置
2019-04-21
vux 选择器_vue中的scoped分析以及在element-UI和vux中的应用
2019-04-21
java实验一目的_Java实验报告(实验一)
2019-04-21
php 内存泄露检测工具,php - 诊断内存泄漏 - 允许#bytes的内存大小耗尽
2019-04-21
Java 去除空格获取文件路径
2019-04-21
python 批量修改文件名称去除文件名中空格
2019-04-21
python 将文件名写入 txt文件
2019-04-21
python 3 读取文件txt 打印print
2019-04-21
python 查找txt文件中的字符串
2019-04-21
python 字符串替换 本地地址转换为网络地址
2019-04-21
Python3 http 服务任意目录 设定访问目录
2019-04-21
Python 移动鼠标到 句柄指定位置
2019-04-21
python窗口置顶 并输入中文
2019-04-21
Android studio 读取sd卡mp3 播放音乐
2019-04-21
Android studio 47 listview 处理单击和长按事件
2019-04-21
android studio 48 Android选项卡TabHost
2019-04-21
android studio 49自定义 ListView
2019-04-21