java - an error occurs when trying to use sharedpreferences editor from mainactivity and subactivity
I want to overwrite data created using Editor in MainActivity using Editor in SubActivity.
Problem i am experiencingIt 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.
// 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.
Environmentwindows10
androidstudio 3.4.1
-
Answer # 1
-
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.javapublic 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
- java - i want to get the entire list from the db and display it, but an error occurs when executing the application
- [vba] an error occurs when trying to save a recordset to adodbstream
- java - i got an error when trying to execute a class that overloaded the constructor
- java - i want to eliminate the error that occurs when starting an application that uses jsonic
- java - an error occurs when executing the jar file
- java - an error occurs when changing a dynamic web module from a static web module with sts (spring-tool-suite)
- java - i want to resolve an error that occurs when running a spring application
- in unity, an error occurs when trying to read a csv file containing a double-byte character string
- java - kod-i want to know what to do when an error occurs in a method that should not have been written
- an error occurs when registering quiz with the wordpress plugin "slick quiz"
- python - data science 100 knocks an error occurs when executing the first cell of the structured data edition
- i got an error when trying to make a calculator with python
- ruby - when posting an image, an error occurs where the image is not defined
- an error occurs when executing the urllibrequesturlopen method of python3
- fighting ice execution error [java]
- iptables: operation not supported error occurs in ufw
- php - error that occurs when laravel follow function undefined variable: user
- i want to resolve a java error
- an error occurs in python scraping from bs4 import beautifulsoup
- java - error: incompatible type: unable to convert string [] to string
- java - i want to set multiple items in the database, but i get an error that the number of arguments does not match
- i don't know about intent in android studio (java)
- java - please tell me how to resolve the android studio error
- java - delayed execution does not work well in android studio (postdelayed error)
- java - how can i make sure that the font size is always full on android?
- assuming that there are 3 android apps [java] buttons and a switch, they are managed by the switch statement button responds, bu
- java - i want to fix an error in android studio
- java - i want to load a kml file into android studio and display the feature data on google map
- java - crash with nullpointerexception when alarm is activated on android o or later
- java - i want to put the data acquired using sqlitedatabase in the text box
In comparison with the main, the sub is probably due to the absence of the first and last line.