Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences getString()返回活动三中的空字符串_Java_Android_Sharedpreferences - Fatal编程技术网

Java SharedReferences getString()返回活动三中的空字符串

Java SharedReferences getString()返回活动三中的空字符串,java,android,sharedpreferences,Java,Android,Sharedpreferences,我有三个活动,LoginActivity.java public class LoginActivity extends AppCompatActivity { TextView txtDaftar; Button btnLogin; EditText inputEmail, inputPassword; // creating constant keys for shared preferences. public static final String SHARED_PREFS = &q

我有三个活动,LoginActivity.java

public class LoginActivity extends AppCompatActivity {

TextView txtDaftar;
Button btnLogin;
EditText inputEmail, inputPassword;

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";

// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

// key for storing email.
public static final String EMAIL_KEY = "email_key";

// key for storing nama.
public static final String NAMA_KEY = "nama_key";

// key for storing role.
public static final String ROLE_KEY = "role_key";

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId, email, nama, role;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    btnLogin = findViewById(R.id.btnLogin);
    txtDaftar = findViewById(R.id.gotoRegister);
    inputEmail = findViewById(R.id.inputEmail);
    inputPassword = findViewById(R.id.inputPassword);

    // getting the data which is stored in shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    userId = sharedpreferences.getString(USER_ID_KEY, "");
    email = sharedpreferences.getString(EMAIL_KEY, "");
    nama = sharedpreferences.getString(NAMA_KEY, "");
    role = sharedpreferences.getString(ROLE_KEY, "");

    txtDaftar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(inputEmail.getText().toString()) || TextUtils.isEmpty(inputPassword.getText().toString())){
                Toast.makeText(LoginActivity.this,"Email / Password Required", Toast.LENGTH_LONG).show();
            }else{
                // Proses login
                login();

            }
        }
    });
}

public void login(){
    LoginRequest loginRequest = new LoginRequest();
    loginRequest.setEmail(inputEmail.getText().toString());
    loginRequest.setPassword(inputPassword.getText().toString());

    Call<LoginResponse> loginResponseCall = ApiClient.getUserService().userLogin(loginRequest);
    loginResponseCall.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if (response.isSuccessful()){
                Toast.makeText(LoginActivity.this,"Login Successful", Toast.LENGTH_LONG).show();
                LoginResponse loginResponse = response.body();

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString(USER_ID_KEY, loginResponse.getUserId());
                editor.putString(EMAIL_KEY, loginResponse.getEmail());
                editor.putString(NAMA_KEY, loginResponse.getNama());
                editor.putString(ROLE_KEY, loginResponse.getRole());
                editor.apply();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (loginResponse.getRole().equals("petani")){
                            Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                },700);
            }else{
                Toast.makeText(LoginActivity.this,"Login Failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Toast.makeText(LoginActivity.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });

}
}
public class DashboardActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";

// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

// key for storing email.
public static final String EMAIL_KEY = "email_key";

// key for storing nama.
public static final String NAMA_KEY = "nama_key";

// key for storing role.
public static final String ROLE_KEY = "role_key";

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId, email, nama, role;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_find_tengkulak, R.id.nav_price, R.id.nav_forecast, R.id.nav_marketplace, R.id.nav_info)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    // initializing our shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);

    // getting data from shared prefs and
    // storing it in our string variable.
    userId = sharedpreferences.getString(USER_ID_KEY, "");
    email = sharedpreferences.getString(EMAIL_KEY, "");
    nama = sharedpreferences.getString(NAMA_KEY, "");
    role = sharedpreferences.getString(ROLE_KEY, "");

    View headerView = navigationView.getHeaderView(0);
    TextView headerNama = (TextView) headerView.findViewById(R.id.header_nama);
    TextView headerEmail = (TextView) headerView.findViewById(R.id.header_email);
    ImageView imgProfil = (ImageView) headerView.findViewById(R.id.vwProfil);

    headerNama.setText(nama);
    headerEmail.setText(email);
    imgProfil.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(DashboardActivity.this, ProfileActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dashboard, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == R.id.action_logout) {
        // calling method to edit values in shared prefs.
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.clear();
        editor.apply();

        Intent intent = new Intent(DashboardActivity.this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);

}
}
public class ProfileActivity extends AppCompatActivity {

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";
// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

TextView editNama, editEmail, editDesa, editKecamatan, editKabupaten, editProvinsi;
Button btnUpdate;

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("Profil");
    }

    // initializing our shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    userId = sharedpreferences.getString(USER_ID_KEY, "");

    // Call function getProfile()
    getProfile(userId);

    editNama = findViewById(R.id.edit_nama);
    editEmail = findViewById(R.id.edit_email);
    editDesa = findViewById(R.id.edit_desa);
    editKecamatan = findViewById(R.id.edit_kecamatan);
    editKabupaten = findViewById(R.id.edit_kabupaten);
    editProvinsi = findViewById(R.id.edit_provinsi);
    btnUpdate = findViewById(R.id.btn_update);
}

public void getProfile(String userId){
    GetProfileRequest getProfileRequest = new GetProfileRequest();
    getProfileRequest.setUser_id(userId);

    Call<GetProfileResponse> getProfileResponseCall = ApiClient.getUserService().userProfile(getProfileRequest);
    getProfileResponseCall.enqueue(new Callback<GetProfileResponse>() {
        @Override
        public void onResponse(Call<GetProfileResponse> call, Response<GetProfileResponse> response) {
            if (response.isSuccessful()){
                GetProfileResponse getProfileResponse = response.body();
                editNama.setText(getProfileResponse.getNama());
                editEmail.setText(getProfileResponse.getEmail());
                editDesa.setText(getProfileResponse.getDesa());
                editKecamatan.setText(getProfileResponse.getKecamatan());
                editKabupaten.setText(getProfileResponse.getKabupaten());
                editProvinsi.setText(getProfileResponse.getProvinsi());
            }
        }

        @Override
        public void onFailure(Call<GetProfileResponse> call, Throwable t) {
            Toast.makeText(ProfileActivity.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
}
ProfileActivity.java

public class LoginActivity extends AppCompatActivity {

TextView txtDaftar;
Button btnLogin;
EditText inputEmail, inputPassword;

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";

// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

// key for storing email.
public static final String EMAIL_KEY = "email_key";

// key for storing nama.
public static final String NAMA_KEY = "nama_key";

// key for storing role.
public static final String ROLE_KEY = "role_key";

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId, email, nama, role;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    btnLogin = findViewById(R.id.btnLogin);
    txtDaftar = findViewById(R.id.gotoRegister);
    inputEmail = findViewById(R.id.inputEmail);
    inputPassword = findViewById(R.id.inputPassword);

    // getting the data which is stored in shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    userId = sharedpreferences.getString(USER_ID_KEY, "");
    email = sharedpreferences.getString(EMAIL_KEY, "");
    nama = sharedpreferences.getString(NAMA_KEY, "");
    role = sharedpreferences.getString(ROLE_KEY, "");

    txtDaftar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(inputEmail.getText().toString()) || TextUtils.isEmpty(inputPassword.getText().toString())){
                Toast.makeText(LoginActivity.this,"Email / Password Required", Toast.LENGTH_LONG).show();
            }else{
                // Proses login
                login();

            }
        }
    });
}

public void login(){
    LoginRequest loginRequest = new LoginRequest();
    loginRequest.setEmail(inputEmail.getText().toString());
    loginRequest.setPassword(inputPassword.getText().toString());

    Call<LoginResponse> loginResponseCall = ApiClient.getUserService().userLogin(loginRequest);
    loginResponseCall.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if (response.isSuccessful()){
                Toast.makeText(LoginActivity.this,"Login Successful", Toast.LENGTH_LONG).show();
                LoginResponse loginResponse = response.body();

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString(USER_ID_KEY, loginResponse.getUserId());
                editor.putString(EMAIL_KEY, loginResponse.getEmail());
                editor.putString(NAMA_KEY, loginResponse.getNama());
                editor.putString(ROLE_KEY, loginResponse.getRole());
                editor.apply();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (loginResponse.getRole().equals("petani")){
                            Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                },700);
            }else{
                Toast.makeText(LoginActivity.this,"Login Failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Toast.makeText(LoginActivity.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });

}
}
public class DashboardActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";

// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

// key for storing email.
public static final String EMAIL_KEY = "email_key";

// key for storing nama.
public static final String NAMA_KEY = "nama_key";

// key for storing role.
public static final String ROLE_KEY = "role_key";

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId, email, nama, role;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_find_tengkulak, R.id.nav_price, R.id.nav_forecast, R.id.nav_marketplace, R.id.nav_info)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    // initializing our shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);

    // getting data from shared prefs and
    // storing it in our string variable.
    userId = sharedpreferences.getString(USER_ID_KEY, "");
    email = sharedpreferences.getString(EMAIL_KEY, "");
    nama = sharedpreferences.getString(NAMA_KEY, "");
    role = sharedpreferences.getString(ROLE_KEY, "");

    View headerView = navigationView.getHeaderView(0);
    TextView headerNama = (TextView) headerView.findViewById(R.id.header_nama);
    TextView headerEmail = (TextView) headerView.findViewById(R.id.header_email);
    ImageView imgProfil = (ImageView) headerView.findViewById(R.id.vwProfil);

    headerNama.setText(nama);
    headerEmail.setText(email);
    imgProfil.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(DashboardActivity.this, ProfileActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dashboard, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == R.id.action_logout) {
        // calling method to edit values in shared prefs.
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.clear();
        editor.apply();

        Intent intent = new Intent(DashboardActivity.this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);

}
}
public class ProfileActivity extends AppCompatActivity {

// creating constant keys for shared preferences.
public static final String SHARED_PREFS = "sahabattani";
// key for storing user_id.
public static final String USER_ID_KEY = "user_id_key";

TextView editNama, editEmail, editDesa, editKecamatan, editKabupaten, editProvinsi;
Button btnUpdate;

// variable for shared preferences.
SharedPreferences sharedpreferences;
String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("Profil");
    }

    // initializing our shared preferences.
    sharedpreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    userId = sharedpreferences.getString(USER_ID_KEY, "");

    // Call function getProfile()
    getProfile(userId);

    editNama = findViewById(R.id.edit_nama);
    editEmail = findViewById(R.id.edit_email);
    editDesa = findViewById(R.id.edit_desa);
    editKecamatan = findViewById(R.id.edit_kecamatan);
    editKabupaten = findViewById(R.id.edit_kabupaten);
    editProvinsi = findViewById(R.id.edit_provinsi);
    btnUpdate = findViewById(R.id.btn_update);
}

public void getProfile(String userId){
    GetProfileRequest getProfileRequest = new GetProfileRequest();
    getProfileRequest.setUser_id(userId);

    Call<GetProfileResponse> getProfileResponseCall = ApiClient.getUserService().userProfile(getProfileRequest);
    getProfileResponseCall.enqueue(new Callback<GetProfileResponse>() {
        @Override
        public void onResponse(Call<GetProfileResponse> call, Response<GetProfileResponse> response) {
            if (response.isSuccessful()){
                GetProfileResponse getProfileResponse = response.body();
                editNama.setText(getProfileResponse.getNama());
                editEmail.setText(getProfileResponse.getEmail());
                editDesa.setText(getProfileResponse.getDesa());
                editKecamatan.setText(getProfileResponse.getKecamatan());
                editKabupaten.setText(getProfileResponse.getKabupaten());
                editProvinsi.setText(getProfileResponse.getProvinsi());
            }
        }

        @Override
        public void onFailure(Call<GetProfileResponse> call, Throwable t) {
            Toast.makeText(ProfileActivity.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
}
public class ProfileActivity扩展了AppCompative活动{
//为共享首选项创建常量键。
公共静态最终字符串共享\u PREFS=“sahabatani”;
//用于存储用户id的密钥。
公共静态最终字符串USER\u ID\u KEY=“USER\u ID\u KEY”;
TextView editNama、editEmail、editDesa、editKecamatan、editKabupaten、editprovansi;
按钮更新;
//共享首选项的变量。
SharedReferences SharedReferences;
字符串用户标识;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
如果(getSupportActionBar()!=null){
getSupportActionBar().setTitle(“Profil”);
}
//初始化我们的共享首选项。
SharedReferences=GetSharedReferences(共享优先,模式专用);
userId=SharedReferences.getString(USER\u ID\u KEY,“”);
//调用函数getProfile()
getProfile(userId);
editNama=findViewById(R.id.edit_nama);
editEmail=findViewById(R.id.edit_email);
editDesa=findviewbyd(R.id.edit\u desa);
editKecamatan=findViewById(R.id.edit_kecamatan);
editKabupaten=findViewById(R.id.edit_kabupaten);
editProvinci=findViewById(R.id.edit\u Provinci);
btnUpdate=findviewbyd(R.id.btn\u更新);
}
public void getProfile(字符串userId){
GetProfileRequest GetProfileRequest=新建GetProfileRequest();
getProfileRequest.setUser_id(userId);
调用getProfileResponseCall=ApiClient.getUserService().userProfile(getProfileRequest);
getProfileResponseCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
GetProfileResponse GetProfileResponse=response.body();
setText(getProfileResponse.getNama());
editEmail.setText(getProfileResponse.getEmail());
setText(getProfileResponse.getDesa());
editKecamatan.setText(getProfileResponse.getKecamatan());
editKabupaten.setText(getProfileResponse.getKabupaten());
setText(getProfileResponse.getProvensi());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(ProfileActivity.this,“Throwable”+t.getLocalizedMessage(),Toast.LENGTH_LONG.show();
}
});
}
}

问题是,当应用程序从LoginActivity移动到DashboardActivity时,我可以检索存储在SharedReference中的值。但是,当我从DashboardActivity移动到ProfileActivity时,我无法获取存储的值,当我调用getString()函数时,它返回默认值,即空字符串。有什么问题吗?

请在
仪表板活动中查看您的功能
onoptions itemselected

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() == R.id.action_logout) {
        // calling method to edit values in shared prefs.
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.clear();
        editor.apply();

        Intent intent = new Intent(DashboardActivity.this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);

}
使用时,可清除此函数中的
SharedReferences

 SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.clear();
    editor.apply();

当您清除它时,您无法从任何位置从
SharedReferences
获取任何数据,这很正常

您是否尝试过调试并查看发生了什么?我尝试过使用
SharedReferences.contains(USER\u ID\u KEY)
函数进行检查,但该函数返回false您是否在进入第三个活动之前注销?我还尝试过将
SharedReferences.contains(USER\u ID\u KEY)
函数放在onCreate()的最后一行方法,返回false。请重新阅读问题,他说,从第二个活动转到第三个活动是问题所在。他也没有从网站注销dashborad@Brendon是的,正如他和你所说:从第二个活动移动到第三个活动是个问题,因为他清除了第二个活动中的共享首选项。他清除的是,在注销菜单中单击,将导航到LoginActivity,而不是ProfileActivity@Brendon对你是right@Brendon也许他按了注销或者从另一个类注销,因为我看不出有任何问题