Home>

I want to overwrite data created using Editor in MainActivity using Editor in SubActivity.

Problem i am experiencing

It works fine until you declare the Editor in MainActivity, but if you declare the Editor in SubActivity, the app will drop at that moment and you will not be able to overwrite the data.
When debugging, the datastore in Sub.java was null.
Looper.java is called when trying to execute Editor editor = dataStore.edit () in goSubb (),

this = com.android.internal.os.ZygoteInit $MethodAndArgsCaller
ex = java.lang.reflect.InvocationTargetException
cause = java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences $Editor android.content.SharedPreferences.edit ()' on a null object reference


The app will close with an error message.
Also, within GoSubb of Sub.java

dataStore = getSharedPreferences ("DataStore", MODE_PRIVATE);


I tried to call the data created with MainActivity in dataStore, but

Cannot resolve method 'getSharedPreferences (java.lang.String, int)'


And getSharedPreferences is in red and cannot be called. I hope you can tell me how to call the data stored in MainActivity in datastore, how to call it to SubActivity with getSharedPreferences, and if there is any wrong sentence.

Applicable source code
// activity_main.xml
<? xml version = "1.0" encoding = "utf-8"?>

    <TextView
        android: id = "@ + id/textView"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: text = "Hello World!"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintLeft_toLeftOf = "parent"
        app: layout_constraintRight_toRightOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" />
    <Button
        android: id = "@ + id/button"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "Button"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"app: layout_constraintHorizontal_bias = "0.517"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/textView"
        app: layout_constraintVertical_bias = "0.174" />
    <Button
        android: id = "@ + id/button3"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "8dp"
        android: text = "Button"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.517"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toBottomOf = "@ + id/button"
        app: layout_constraintVertical_bias = "0.124" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.intenttest;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private Sub sub;
    SharedPreferences dataStore;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        dataStore = getSharedPreferences ("DataStore", MODE_PRIVATE);
        Editor editor = dataStore.edit ();
        String fragmentId = "I will send";
        editor.putString ("fragmentId", fragmentId);
        editor.commit ();
        Button go = findViewById (R.id.button);
        Button ret = findViewById (R.id.button3);
        String res = "";
        sub = new Sub (MainActivity.this);
        go.setOnClickListener (new View.OnClickListener () {
            @Overridepublic void onClick (View view) {
                sub.goSubb (MainActivity.this);
                TextView kekka = findViewById (R.id.textView);
                String str = dataStore.getString ("fragmentId", "Nothing");
                kekka.setText (str);
            }
        });
        ret.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick (View view) {
                sub.ret (MainActivity.this);
            }
        });
    }
}
Sub.java
package com.example.intenttest;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.TextView;
import static android.app.Activity.RESULT_OK;
class Sub {
    SharedPreferences dataStore;
    String res = "";
    Sub (MainActivity mainActivity) {
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
    }
    public void goSubb (MainActivity mainActivity) {
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
        Editor editor = dataStore.edit ();
        String fragmentId2 = "half successful";
        editor.putString ("fragmentId", fragmentId2);
        String ketu = dataStore.getString ("fragmentId", "Nothing");
    }

    public void ret (MainActivity mainActivity) {
        TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
        kekka.setText ("kaeri" + res);
    }
}
Surveyed by myself

I looked for data calling and data overwriting in another Activity, but there was a site that taught until the data calling, but I could not call it well with my knowledge, I found overwriting did not.

Environment

windows10
androidstudio 3.4.1

  • Answer # 1

    In comparison with the main, the sub is probably due to the absence of the first and last line.

    dataStore = getSharedPreferences ("DataStore", MODE_PRIVATE);
            Editor editor = dataStore.edit ();
            String fragmentId = "I will send";
            editor.putString ("fragmentId", fragmentId);
            editor.commit ();

  • Answer # 2

    public void goSubb (MainActivity mainActivity) {
            TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
            Editor editor = dataStore.edit ();
            String fragmentId2 = "half successful";
            editor.putString ("fragmentId", fragmentId2);
            String ketu = dataStore.getString ("fragmentId", "Nothing");
        }


    This part in Sub.java

    public void goSubb (MainActivity mainActivity) {
            TextView kekka = (TextView) mainActivity.findViewById (R.id.textView);
            dataStore = mainActivity.dataStore;
            Editor editor = dataStore.edit ();
            String fragmentId = "half success";
            editor.putString ("fragmentId", fragmentId);
            String ketu = dataStore.getString ("fragmentId", "Nothing");
            editor.commit ();
        }


    In this way, I was able to call it safely. Thank you for your answer, advice.

Related articles