I entered my username here and saved as pref in sharedPreferences
private void saveUserName(){
String userName = mUsernameView.getText().toString();
SharedPreferences prefs = getSharedPreferences(PREFS, 0);
prefs.edit().putString(DISPLAY_NAME_KEY, userName).apply();
}
Retrieving it here and trying to saved it to a textview but getting error private void setUserName(){
String mSetUserName;
SharedPreferences prefs = getSharedPreferences(RegisterClass.PREFS, MODE_PRIVATE);
mSetUserName = prefs.getString(RegisterClass.DISPLAY_NAME_KEY, null);
if (mSetUserName == null) mSetUserName ="Anonymous";
userName.setText(mSetUserName);
The error is pointing to where i used the setText on my username, saying it is pointing to a null value, which is untrue, I registered with a user name.
FATAL EXCEPTION: main
Process: com.onyebuchboss.bossweatherbitcoinapp, PID: 1553
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onyebuchboss.bossweatherbitcoinapp/com.onyebuchboss.bossweatherbitcoinapp.OptionActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6293)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1094)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.onyebuchboss.bossweatherbitcoinapp.OptionActivity.setUserName(OptionActivity.java:84)
at com.onyebuchboss.bossweatherbitcoinapp.OptionActivity.onCreate(OptionActivity.java:36)
I declared the userName in the onCreate method here
mWeatherBtn = (Button) findViewById(R.id.weatherAppButton);
mBitcoinBtn = (Button) findViewById(R.id.bitcoinAppButton);
userName = (TextView) findViewById(R.id.userName);
shareBtn = (Button) findViewById(R.id.shareBtn);
The Xml filefor the userName
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/default_name"
android:textSize="20dp"/>
<Button
android:id="@+id/weatherAppButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/bitcoinAppButton"
android:layout_alignLeft="@+id/bitcoinAppButton"
android:layout_alignStart="@+id/bitcoinAppButton"
android:layout_marginBottom="86dp"
android:background="@android:color/black"
android:text="@string/change_for_WeatherApp"
android:textColor="@android:color/white"
android:textSize="40sp"/>
Entire code for OptionActivity
public class OptionActivity extends AppCompatActivity {
Button mWeatherBtn;
Button mBitcoinBtn;
TextView userName;
Button shareBtn;
String TAG = "Weather & Bitcoin App";
String shareString = "Please download this Awesome App";
//private String mSetUserName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option_layout);
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
setUserName();
mWeatherBtn = (Button) findViewById(R.id.weatherAppButton);
mBitcoinBtn = (Button) findViewById(R.id.bitcoinAppButton);
userName = (TextView) findViewById(R.id.userName);
shareBtn = (Button) findViewById(R.id.shareBtn);
mWeatherBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent weatherIntent = new Intent(OptionActivity.this, WeatherActivity.class);
//finish();
startActivity(weatherIntent);
}
});
mBitcoinBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent bitcoinIntent = new Intent(OptionActivity.this, BitcoinActivity.class);
startActivity(bitcoinIntent);
}
});
shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, TAG);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareString);
startActivity(shareIntent);
}
});
}
//Retrieve the user name from shared preferences
private void setUserName(){
String mSetUserName;
SharedPreferences prefs = getSharedPreferences(RegisterClass.PREFS, MODE_PRIVATE);
mSetUserName = prefs.getString(RegisterClass.DISPLAY_NAME_KEY, null);
if (mSetUserName == null) mSetUserName ="Anonymous";
userName.setText(mSetUserName);
}
}
Move setUserName()
below declaration, right now you are calling settext on userName
before initialising it
mWeatherBtn = (Button) findViewById(R.id.weatherAppButton);
mBitcoinBtn = (Button) findViewById(R.id.bitcoinAppButton);
userName = (TextView) findViewById(R.id.userName);
shareBtn = (Button) findViewById(R.id.shareBtn);
setUserName();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments