如何利用全球一流的机器人variales [英] how to use global class variales in android
本文介绍了如何利用全球一流的机器人variales的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我在做一个应用程序在这里我使用varibles.its工作以及全球一流的,但如果我用更多的globalclass变量,我收到内存异常使用一些再寄一次尝试这是正确的方式或不是任何一个建议我。 ......
公共类二等延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.second);
TextView的电视=(TextView中)findViewById(R.id.tv);
tv.setText(环球字符串:+ GlobalClass.myVal);
}
}
类GlobalClass扩展应用{
静态字符串设为myVal;
}
解决方案
第一件事情,你不需要静态变量在应用类中声明全局变量,
因此改变你的code到:
类GlobalClass扩展应用{
公共字符串设为myVal;
}
那么。无论您需要访问这些数据,以获得适用对象:
GlobalClass全球=(GlobalClass)context.getApplication();
global.myVal =东西;
hi i am doing one app here i am using global class varibles.its working well,but if i using more globalclass variables i am getting memory exception some time.i tried using is this correct way or not any one suggest me....
public class SecondClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Global String : " + GlobalClass.myVal);
}
}
class GlobalClass extends Application {
static String myVal;
}
解决方案
First thing, you dont need Static Variable to declare global variable in Application Class,
so Change your code to:
class GlobalClass extends Application {
public String myVal;
}
then whereever you need to access this data, get Application object by:
GlobalClass global=(GlobalClass)context.getApplication();
global.myVal="anything";
这篇关于如何利用全球一流的机器人variales的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文