首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android 全局变量使用有关问题

2013-03-17 
android 全局变量使用问题全局定义变量在登录页设置值后,在登录后跳转到的页面上获取不到值,一直为空。全局

android 全局变量使用问题
全局定义变量在登录页设置值后,在登录后跳转到的页面上获取不到值,一直为空。

全局变量的定义


public class MyApplication extends Application {

    private Map map;
    private Connection.Response response;

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Connection.Response getResponse() {
        return response;
    }

    public void setResponse(Connection.Response response) {
        this.response = response;
    }

}



AndroidManifest.xml 配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test.login"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="10"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:label="@string/app_name"
                 android:name="com.test.common.MyApplication"
                 android:icon="@drawable/logo">
        <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="com.test.index.IndexActivity">
            <intent-filter>


            </intent-filter>
        </activity>
        <activity android:name="com.test.show.ShowActivity">
            <intent-filter>


            </intent-filter>
        </activity>
    </application>
</manifest> 



从MainActivity跳转到IndexActivity



 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       .....
       .....
        MyApplication myApplication = new MyApplication();
        myApplication.setResponse(response);
        myApplication.setMap(response.cookies());
       ......
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, IndexActivity.class);
        startActivity(intent);
}

 


获取全局变量

   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        MyApplication myApplication = (MyApplication) getApplication();
        System.out.println( myApplication.getResponse());
    }

结果怎么都取不到。。
很是郁闷,高手请指点下,迷茫一下午了!!
[解决办法]
没set进去啊,怎么可能取得到。
在MyApplication里继承个 onCreate方法,里面调用 setResponse(..) set一个进去。

虽然我不知道你这么做的目的是什么。
[解决办法]
引用:
引用:
没set进去啊,怎么可能取得到。
在MyApplication里继承个 onCreate方法,里面调用 setResponse(..) set一个进去。

虽然我不知道你这么做的目的是什么。
额是不是我弄错了我new了个新对象
MyApplication myApplication = new MyApplication();
应该直接get……


应该是这样

[解决办法]
引用:
引用:没set进去啊,怎么可能取得到。
在MyApplication里继承个 onCreate方法,里面调用 setResponse(..) set一个进去。

虽然我不知道你这么做的目的是什么。
额是不是我弄错了我new了个新对象
MyApplication myApplication = new MyApplication(……

你已经知道有这个可能了 怎么不试一下
[解决办法]
Application不是你自己new的,系统会帮你new的,你要存全局变量,写成静态的就行了,直接取

热点排行