[英]can someone help me with my global variables?
好的,我正在创建一个使用普通全局类的程序,但它无法正常工作。 我可以从全局获取变量的值,但无法设置新值。
我创建了一个非常短的测试程序只是为了让它工作,这样我就可以将它更改为我的另一个更大的程序。
在这个小程序中,我有一个文本视图和一个编辑文本,中间有一个按钮
它的设计目的是在单击按钮时获取在 editText 中键入的内容并将其放入变量中并将该变量发送到全局变量
然后同时从全局变量中获取变量并将 textView 文本设置为该变量...EASY...
但它不会保存到全局变量...
有人能帮助我吗?
这是我正在使用的全局代码。
package com.example.arnoldray007.globalstest;
/**
* Created by arnoldray007 on 5/3/2016.
*/
public class Globals
{
private static Globals instance;
//this is the global variable
private String received;
//this restricts the constructor from being instantiated
private Globals(){}
public void setReceived(String r)
{
this.received = r;
}
public String getReceived()
{
return this.received;
}
//this is the global variable
private String send = "Sorry im busy right now.";
public void setSend(String s)
{
this.send = s;
}
public String getSend()
{
return this.send;
}
public static synchronized Globals getInstance()
{
if(instance == null)
{
instance = new Globals();
}
return instance;
}
这是我正在使用的 MainActivity 代码
public class MainActivity extends AppCompatActivity
{
Globals g = Globals.getInstance();
String received = g.getReceived(); //this retrieves the variable
Button button;
EditText editText;
TextView textView;
String temp;
public void myClickThree(View v)//this is my onclickThree for my refresh button
{
temp = editText.getText().toString();
g.setReceived(temp); //this sends the new message to the global variable adding it to it
received = g.getReceived();
editText.setText(received);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
}
}
感谢我能得到的任何帮助......
我现在收到一个错误,谁能告诉我这是什么意思?
05-05 09:41:47.476 9601-9601/com.example.arnoldray007.arnoldfinalproject E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arnoldray007.arnoldfinalproject/com.example.arnoldray007.arnoldfinalproject.Received_messages}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.widget.TextView.append(TextView.java:3118)
at com.example.arnoldray007.arnoldfinalproject.Received_messages.onCreate(Received_messages.java:91)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
在单例模式中 getInstance() 应该是:
private static Globals instance;
public static Globals getInstance() {
if (instance == null)
instance = new Globals();
return instance;
}
您确定在 Globals 类主体中有这个确切的代码吗?
编辑:
我认为对于此类问题,最好的解决方案是使用超类 Application,您可以在其中管理所有全局内容。 你会发现数以千计的关于这个 android 编程模式的教程,只需谷歌“扩展应用程序 android”。
您显示的代码没有任何问题,只是您的评论似乎具有误导性。
这将新消息发送到全局变量添加它
没有任何东西被添加到任何东西中。 因为你的方法只做一个任务。
public void setReceived(String r)
{
this.received = r;
}
如果您想查看您的类实际上是否在工作,也许您应该做的不仅仅是从 EditText 获取文本并将字符串分配回 EditText。 那只会看起来好像什么都没发生。
所以,在onCreate()
试试这个
g = Globals.getInstance();
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String temp = editText.getText().toString();
g.setReceived(temp);
received = g.getReceived();
textView.append("\n"+received);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.