-1

I have a settings activity in android studio where a user will enter a name in a text box. They will click save and go to another activity and their name will show in a textview. I'm having trouble trying to get the name to show in the textview, I've tried a few different things and have gotten a few different errors, it's probably something simple I'm not getting or overlooking. Here is my error+code

public class ActivityDuel extends AppCompatActivity {


public static String getPrefs(String n, Context context) {
    final TextView txtD1 = (TextView) getView().findViewById(R.id.txtD1);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(n, "name");
    txtD1.setText(n);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



}

}

My error is :

error: non-static method <T>findViewById(int) cannot be referenced from a static context
where T is a type-variable:
T extends View declared in method <T>findViewById(int)
Zoe
  • 27,060
  • 21
  • 118
  • 148
john c
  • 65
  • 1
  • 7
  • Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Zoe May 21 '19 at 19:31
  • follow this answer to solve your problem ;- https://stackoverflow.com/questions/56246550/how-i-can-passing-data-from-one-activity-to-multiple-activities/56250003?noredirect=1#comment99117992_56250003 – Sandeep Malik May 22 '19 at 06:14

4 Answers4

2

Remove static keyword and return your saved String

public String getPrefs(String n, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(n, "name");
}

on your OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final TextView txtD1 = (TextView) findViewById(R.id.txtD1);
    txtD1.setText(getPrefs("your_key", this));

}
shb
  • 5,957
  • 2
  • 15
  • 32
  • thank you, but it is only returning the default value on return preferences.getString(n, "name"); for some reason now in the text view – john c May 22 '19 at 17:53
  • Did you save any String in SharedPreferences? if you did use the key you used while saving it. and replace with "your_key" in onCreate – shb May 22 '19 at 18:53
2

Just want to add to the other answers to explain why this doesn't work -- the problem is you are using the static keyword on your getPrefs method.

Since getPrefs is a static method, it belongs to the ActivityDuel class, but not the ActivityDuel instance. So it can't reference your TextView, because that's part of the activity instance.

Here's a good explanation of statics in java

PhillyTheThrilly
  • 1,562
  • 2
  • 16
  • 21
0

try to achieve it this way :

public class ActivityDuel extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    Toolbar toolbar = findViewById(R.id.toolbar);
    TextView txtD1 = (TextView) findViewById(R.id.txtD1);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String string = preferences.getString(n, "name");
    setSupportActionBar(toolbar);

    txtD1.setText(string);




  }  
}
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
0

Assuming you have a static fragment inner class inside an activity: you're trying to call the activity's findViewById() which you cannot in a static inner class that doesn't hold a reference to the parent.

In onCreateView() you need to call it on the root view you just inflated.

You can try this.

public class ActivityDuel extends AppCompatActivity {


public String getPrefs(String n, Context context) {
    final  TextView txtD1 = (TextView) rootView.findViewById(R.id.txtD1);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(n, "name");
    txtD1.setText(n);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_duel);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



}

}
David Kariuki
  • 1,522
  • 1
  • 15
  • 30