-5

my logical I am trying to get a textView from my Main.java file, and when the notification is clicked, it should print it. I know I am to use sharedPreferences for that. I did that. However, it keeps throwing a NullPointerException, but I have called everything i need to call in my NotificationPage.java class so I am not sure what exactly is wrong. Please any help is appreciated.

This is my NotificationPage.java class

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;


public class NotificationPage extends Activity {
SharedPreferences sp;
SharedPreferences.Editor edit;
TextView arrival;
TextView notifdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);
    notifdate=(TextView)findViewById(R.id.textView5);
    arrival=(TextView)findViewById(R.id.textView4);
    sp=getSharedPreferences(arr, MODE_PRIVATE);
    edit=sp.edit();
    sp.getString(arr, null);
    edit=sp.edit();
    edit.putString(arr,sp.getString(arr,null));
    notifdate.setText("Hey!" + sp.getString(arr,null));
    edit.apply();
}

this is the line of code that gets my shared preferences from my other java file

arr=arrival.getText().toString();
sp=getSharedPreferences(arr,MODE_PRIVATE);
gobernador
  • 5,659
  • 3
  • 32
  • 51
Aria
  • 389
  • 3
  • 7
  • 25
  • "However, it keeps throwing a NullPointerException" -- please post the entire stack trace. – CommonsWare Sep 10 '15 at 00:30
  • From the stacktrace it doesn't look like your issue has anything to do with sharedPreferences. The NPE is being thrown on the arrival.getText(), which indicates that the arrival is null – Leo Sep 10 '15 at 01:24
  • Why don't you use a static word for the "Session Name" ? Is there any problem using static word? – Nic Sep 10 '15 at 01:26
  • @leo it shouldn't be null, when the user inputs data, it prints it on the TextView 'arrival'. So when the notification comes to the phone, it shows the TextView but on a different activity – Aria Sep 10 '15 at 01:38
  • 1
    You are still getting from TextView... From what i see your error will be the arrival.getText().toString(); << is getting null.. Maybe you try your "Session Name" as static string. Example "MyArrival". You try to check textview - if (arrival != null) arr = arrival.getText().toString(); else arr = "arrival"; – Nic Sep 10 '15 at 01:38
  • still caused an error – Aria Sep 10 '15 at 01:58

3 Answers3

1

Initialize your shared Preference object (sp)

sp = getSharedPreferences("Session Data", Context.MODE_PRIVATE);    
meda
  • 45,103
  • 14
  • 92
  • 122
  • I put an example you need to initialize the object before you use it,I trust you that you can figure it out – meda Sep 10 '15 at 00:45
  • but i did that after initializing my TextView :/ – Aria Sep 10 '15 at 01:11
  • no you did something differernt, look into `R.layout.notification` and make sure every id matches your views, the problem might be the textview – meda Sep 10 '15 at 01:13
  • there is only one TextView in my notification.xml, the second one I declared is from another xml file, which the sharedPreferences should get. – Aria Sep 10 '15 at 01:16
1

private final String SP_FILE = "my_shared_preferences";

You have no idea how to use the shared preferences. Please refer to the Shared Preference Guide.

You have 2 ways to use Shared Preferences, reading or writing.

If you write to your shared preferences, first give a name to it.

public static final String SP_FILE_NAME = "my_shared_preferences";

SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
sp.edit().putString("key", "value").apply();

There is one edit for one apply. In between you can put what you want.

To read the value:

SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
String value = sp.getString("key", "defaultValue");

Hope you can see what is wrong in your code.

xiaomi
  • 6,553
  • 3
  • 29
  • 34
  • ok I see what I did wrong, it stopped crashing, so that worked! thank you! the problem i have though is that it does not get the Text from the Main.java file. see when I open the notification, the TextView in the NotificationPage.java that should reflect the other TextView from the Main.java file comes up blank. Would you have any idea what I should do to fix that? – Aria Sep 10 '15 at 02:13
1

Put a key-value

public static final String SP_NAME = "my_preferences";
SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE);
sp.edit().putString("key", "value").apply();

Get a value

SharedPreferences sp = getSharedPreferences(SP_FILE_NAME, MODE_PRIVATE);
String value = sp.getString("key", "defaultValue");
gobernador
  • 5,659
  • 3
  • 32
  • 51
J.Chao
  • 26
  • 3
  • I'm sorry, I keep making mistakes with sharedPreferences but yours worked perfectly for me. thank you so much! – Aria Sep 10 '15 at 02:54