-1

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) 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

1

in singleton pattern getInstance() should be:

private static Globals instance;

public static Globals getInstance() {
    if (instance == null)
        instance = new Globals();
    return instance;
}

Are you sure to have this exact code inside Globals class body?

EDIT:

I think for this kind of problem the best solution is using a superclass Application, where you mantain all the global stuffs. You will find thousands of tutorial on this android programming pattern, just google "extending application android".

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34
0

There isn't anything wrong with the code you've shown except your comment seems misleading.

this sends the new message to the global variable adding it to it

Nothing is being added to anything. Because your method there only does an assignment.

public void setReceived(String r)
{
    this.received = r;
}

If you want to see that your class is actually working, maybe you should do something more than get text from an EditText and assign the string back into the EditText. That will only appear like nothing is happening.

So, try this instead in 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);
    }
});
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks a lot, this works great on my test application, but when I try to implement it on my real application it pops up with an error – Danny Arnold May 05 '16 at 14:39
  • The error `java.lang.NullPointerException at android.widget.TextView.append` means you didn't initialize your TextView or findViewById returned null – OneCricketeer May 05 '16 at 15:05
  • I don't understand , because I did. here is what I have for the display( EditText editText9, changeEditText;)---(changeEditText = (EditText) findViewById(R.id.changeEditText);)---rember that I said that the error was coming from my real program not the test program..... the test program works fine with the answer below... but when I ad the fix to my program it gives me this error... – Danny Arnold May 06 '16 at 13:12
  • That's an EditText, not a Textview. Anyways. I'm not sure why you'd get an error in one device, but not another. I'm just telling you what the error means – OneCricketeer May 06 '16 at 13:30