Ok, I am creating a program that uses a normal global class but it doesn't seam to work. I can get the value of variables from the global but I cant set a new value.
I created a really short test program just to get this to work so I can change it to my other much bigger program.
In this small program I have a textview and a edit text with a button in the middle
all it is designed to do is when the button is clicked take what was typed in the editText and place it in a variable and send that variable to globals
then at the same time get the variable from globals and set the textView text to that variable...EASY...
but it will not save to globals...
can someone help me please?
here is the global code that I am using.
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;
}
and here is the MainActivity code that I am using
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);
}
}
Thanks for any help that I can get......
I am getting an error now can anyone tell me what this means?
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)