使用全局变量在Activity之间传递数据

今天在学习Activity使用全局变量传递数据时遇到了许多问题,现在总结一下:

       使用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是:Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。

   


这是我工程的截图

主方法:Main.java

package com.android.life;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
    /** Called when the activity is first created. */
private Button button;
private MyApp myApp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myApp = (MyApp)getApplication();
myApp.setName("jack");//修改之后的名称
Intent intent = new Intent(Main.this,OtherActivity.class);
startActivity(intent);
}
});
    }
}

OtherActivity.java


package com.android.life;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class OtherActivity extends Activity {


private MyApp myApp;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView)this.findViewById(R.id.msg);
myApp = (MyApp)getApplication();
textView.setText("-appname-->>"+myApp.getName());
}
}

MyApp.java


package com.android.life;


import android.app.Application;


public class MyApp extends Application {


public String name;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("张三");
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="使用Application传递数据" />
</LinearLayout>


other.xml


<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" 
        android:id="@+id/msg"/> </LinearLayout>


strings.xml

<resources>
    <string name="hello">Hello World,Main</string>
    <string name="app_name">android 的生命周期演示</string>
</resources>



AndroidMainfest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.life"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />


    <application
        android:name=".MyApp"  
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
 >
  <activity android:name=".Main" 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>


如果出现类转换失败:java.lang.ClassCastException: android.app.Application

记得在清单文件中加上MyApp的声明,如上红字描述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值