我该如何解决 java.lang.NullPointerException 问题?
How can i solve java.lang.NullPointerException problem?
我的 Main 中有两个编辑文本和一个按钮Activity 其中一个编辑文本获取用户名信息,另一个获取密码。当我单击该按钮时,它会将我传送到另一个 activity,在这个 activity 中有一个文本视图和一个按钮,该文本视图显示密码和用户名信息,当我单击该按钮时,它应该删除用户名和密码 editor.remove 并将用户带到 MainActivity 但是当我单击该按钮时程序正在关闭。
我的主要Activity:
package com.example.sploginekran;
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;
import android.widget.Toast;
import javax.microedition.khronos.egl.EGLDisplay;
public class MainActivity extends AppCompatActivity {
Button buttonGiris;
EditText editTextName,editTextPassword;
SharedPreferences sp;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonGiris = findViewById(R.id.buttonGiris);
editTextName = findViewById(R.id.editTextName);
editTextPassword = findViewById(R.id.editTextPassword);
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
editor = sp.edit();
buttonGiris.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextName.getText().toString().equals("admin") &&
editTextPassword.getText().toString().equals("123")) {
editor.putString("username",editTextName.getText().toString());
editor.putString("password",editTextPassword.getText().toString());
editor.commit();
Intent intent = new Intent(MainActivity.this, AnaEkranActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(),"Giriş Hatalı",Toast.LENGTH_SHORT).show();
}
}
});
}
}
我的第二个Activity:
package com.example.sploginekran;
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.TextView;
public class AnaEkranActivity extends AppCompatActivity{
Button buttonCikisYap;
TextView textViewCikti;
SharedPreferences sp;
SharedPreferences.Editor editor;
String username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_ekran);
buttonCikisYap = findViewById(R.id.buttonCikisYap);
textViewCikti = findViewById(R.id.textViewCikti);
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
sp.edit();
username = sp.getString("username","Kullanıcı Adı Yok");
password = sp.getString("password","Şifre Yok");
textViewCikti.setText(username+"-"+password);
buttonCikisYap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.remove("username");
editor.remove("password");
editor.commit();
startActivity(new Intent(AnaEkranActivity.this,MainActivity.class));
}
});
}
}
我得到的错误:
java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
此行的第二个 Activity 中也出现错误:
editor.remove("username");
在你的第二个 activity AnaEkranActivity
。你有这个代码:
SharedPreferences.Editor editor;
@Override
protected void onCreate() {
...
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
sp.edit(); // YOU MISSED ASSIGNING editor HERE
...
buttonCikisYap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.remove("username"); // editor IS NULL HERE
...
您错过了将对象分配给编辑器变量。这就是你得到空指针异常的原因。
修复:
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
editor = sp.edit();
我的 Main 中有两个编辑文本和一个按钮Activity 其中一个编辑文本获取用户名信息,另一个获取密码。当我单击该按钮时,它会将我传送到另一个 activity,在这个 activity 中有一个文本视图和一个按钮,该文本视图显示密码和用户名信息,当我单击该按钮时,它应该删除用户名和密码 editor.remove 并将用户带到 MainActivity 但是当我单击该按钮时程序正在关闭。
我的主要Activity:
package com.example.sploginekran;
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;
import android.widget.Toast;
import javax.microedition.khronos.egl.EGLDisplay;
public class MainActivity extends AppCompatActivity {
Button buttonGiris;
EditText editTextName,editTextPassword;
SharedPreferences sp;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonGiris = findViewById(R.id.buttonGiris);
editTextName = findViewById(R.id.editTextName);
editTextPassword = findViewById(R.id.editTextPassword);
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
editor = sp.edit();
buttonGiris.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextName.getText().toString().equals("admin") &&
editTextPassword.getText().toString().equals("123")) {
editor.putString("username",editTextName.getText().toString());
editor.putString("password",editTextPassword.getText().toString());
editor.commit();
Intent intent = new Intent(MainActivity.this, AnaEkranActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(),"Giriş Hatalı",Toast.LENGTH_SHORT).show();
}
}
});
}
}
我的第二个Activity:
package com.example.sploginekran;
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.TextView;
public class AnaEkranActivity extends AppCompatActivity{
Button buttonCikisYap;
TextView textViewCikti;
SharedPreferences sp;
SharedPreferences.Editor editor;
String username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_ekran);
buttonCikisYap = findViewById(R.id.buttonCikisYap);
textViewCikti = findViewById(R.id.textViewCikti);
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
sp.edit();
username = sp.getString("username","Kullanıcı Adı Yok");
password = sp.getString("password","Şifre Yok");
textViewCikti.setText(username+"-"+password);
buttonCikisYap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.remove("username");
editor.remove("password");
editor.commit();
startActivity(new Intent(AnaEkranActivity.this,MainActivity.class));
}
});
}
}
我得到的错误:
java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference
此行的第二个 Activity 中也出现错误:
editor.remove("username");
在你的第二个 activity AnaEkranActivity
。你有这个代码:
SharedPreferences.Editor editor;
@Override
protected void onCreate() {
...
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
sp.edit(); // YOU MISSED ASSIGNING editor HERE
...
buttonCikisYap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.remove("username"); // editor IS NULL HERE
...
您错过了将对象分配给编辑器变量。这就是你得到空指针异常的原因。
修复:
sp = getSharedPreferences("Bilgiler",MODE_PRIVATE);
editor = sp.edit();