I am trying to use sharedPreferences from my Register Page and retrieve it on another class, but it is giving me error in the logcat

Victor Onyebuchi

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);
    }
}
Navneet Krishna

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.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to build my game in unity but it is giving me an error

I am trying to use amazon api code in php but it is giving me error of not supported version following is my code

I am trying to run a task in my async method but it's giving me a ui thread error

I am trying to upload the captured/ selected image from gallery to my server but it is giving below error

I am getting NAN error when trying to retrieve a value from a class?

when i am trying to call a servlet page from my jsp page using anchor tag it showing an error

Why am I obtaining undefined trying to retrieve an array from this class?

I am getting error in my Logcat and my app is not running its crashing, Help me please

I am trying to update a value in an observable every second in setInterval but mobx is giving me error

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

I am trying to get the top 5 products using a Mysql Query but it's giving me an error

Why am I getting a type error when trying to use a struct from a header in my .c file?

I am trying to retrieve data from my firebase database using getter and setter but getting an error says can't convert the object

I am trying to connect my web page to my local server database but I am getting error ECONNREFUSED

I am trying to register a staff in my database throuout the form But I get error

i am trying to print words from a sentence in c++ but its giving me Segmentation Fault(core dump)

I am trying to call a function from useEffect hook and it's giving me undefined

when i try to migrate my profile table its giving me the error .i am using laravel 5.1

I am trying to use a method which is in different class but it generates an error

I am trying to extract only date value from Oracle's SYSDATE but my query is giving me ORA-02290: check constraint violated

When i am trying to use @ResController in my maven project it showing error " RestController can not be resolved to a type".Help me to resolve this

I am getting an error, while adding DesignGridLayout to my register form. Please help me with this

flutter error while trying to retrieve a Map from sharedPreferences

When i am using it with db2 for pagination, my next page is giving error

I'm trying to use GLU.gluPerspective(), but it's giving me the static reference error

I can render my page just once. After refreshing the page, i am getting an error as cant find data from api. Please let me know my mistake

I am trying to use a bottom sheet modal in flutter conditionally only when users are not subscribed but it's giving me errors

phantomjs: my page.evaluate(function()) is not taking the paramters i pass it as values and giving me null error

I am trying to make a SQL query of Update in python but it is giving error

TOP Ranking

HotTag

Archive