сохранение данных в разных фрагментах

У меня есть активность button navigation activityс 3 вкладками. На первой вкладки есть кнопка которая делает переход на новую активность. В этой активности есть Edittext куда вводятся данные. После ввода данных на этой же активности есть кнопка которая возвращает в button navigation и эта же кнопка сохраняет данные. Я возвращаюсь на первую вкладку, а данные которые у меня сохраняются должны отображаться на 3 вкладке. Но когда я ввожу Textview который находится на 3 вкладке у меня просто вылетает приложение. Я решил проверить работает ли сам SharedPreferences и сделал textView на 1 вкладке где кнопка. ТУда она выводит данные и не вылетает ,но если я меняю этот TextView на текст который на 3 вкладке у меня вылетает приложение. Как мне можно перенести данные с 1 на 3 вкладку...

package my.file.testlast;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {
TextView textView;
TextView textView2;
    SharedPreferences pref;
    private static final String SHARE = "mypref";
    private static final String Name = "name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        textView = (TextView) findViewById(R.id.textView);
        textView2  = (TextView) findViewById(R.id.textView2);
        pref = getSharedPreferences(SHARE,MODE_PRIVATE);
        String name = pref.getString(Name, "");
        if(name != null){
        textView.setText(name);

    }}



    public void click(View view) {
        Intent intent= new Intent(MainActivity.this, trade.class);
        startActivity(intent);

    }

(textview находится на первой вкладке ,а textView2 на 3 вкладке и если я меняю строку textView.setText(name); на строку textView2.setText(name); то у меня вылетает приложение) Ниже представлен фрагмент отправляющей активности.

package my.file.testlast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class trade extends AppCompatActivity {

    EditText editText;
    Button button;
SharedPreferences pref;
    private static final String SHARE = "mypref";
    private static final String Name = "name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade);

        editText = (EditText) findViewById(R.id.editTextTextPersonName);
        button = (Button) findViewById(R.id.button2);
        pref = getSharedPreferences(SHARE, MODE_PRIVATE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(Name, editText.getText().toString());
                editor.apply();
                Intent inten = new Intent(trade.this, MainActivity.class);
                startActivity(inten);
            }
        });
    }

Ответы (0 шт):