好的,我正在创建一个使用普通全局类的程序 但它没有缝合工作。我可以从全局中获取变量的值,但我无法设置新的值。
我创建了一个非常简短的测试程序,只是为了让它工作,所以我可以把它改成我的其他更大的程序。
在这个小程序中,我有一个textview和一个带有中间按钮的编辑文本
所有它的设计目的都是在单击按钮时将editText中输入的内容放入变量并将该变量发送给全局
然后同时从globals获取变量并将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)
答案 0 :(得分:1)
getInstance()应为:
.a
你确定在Globals类体中有这个确切的代码吗?
编辑:
我认为对于这类问题,最好的解决方案是使用超类应用程序,在那里你保留所有全局的东西。你会在这个android编程模式上找到成千上万的教程,只需google"扩展应用程序android"。
答案 1 :(得分:0)
您所显示的代码没有任何问题,但您的评论似乎有误导性。
这会将新消息发送到将其添加到其中的全局变量
什么都没有添加到任何东西。因为你的方法只有一个任务。
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);
}
});