当我执行代码时,它给了我一个错误,我尝试做的是尝试以某种方式获取用户在alertDialog中插入的值(字符串)。 错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText()”
这是我的.java
public void verificar(){
SharedPreferences sharedPref = getSharedPreferences("PREFS",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
EditText b = findViewById(R.id.userName);
String nombre = b.getText().toString(); //Obtengo el nombre que se inserto en el cuadro
editor.putInt("lastScore",cont);
editor.putString("lastNombre",nombre);
editor.commit();
Intent intent = new Intent(getApplicationContext(),Ranking.class);
startActivity(intent);
}
这是我的 XML,带有 EditText
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Usted esta en el top10!. Desea figurar?"
android:textSize="20sp"
android:textAlignment="center"
/>
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:inputType="textPersonName"
android:textSize="20sp"
android:layout_marginTop="10dp"
/>
这是具有“verificar”的方法
public void deseaFigurar(){
final AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog, null))
.setPositiveButton("Figurar" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
verificar();
finish();
}
})
.setNegativeButton("No figurar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(MainActivity.this,Ranking.class);
startActivity(intent);
}
});
builder.show();
}
最佳答案
当您将 findViewById(R.id.userName)
与对话框中的 View 一起使用时,您需要通过对话框来查找它,我不知道您何时以及如何初始化对话框,但应该像这样使用它:dialog.findViewById(R.id.userName)
。您可以通过更改函数参数(如下面的代码)来快速修复它,并在调用时将对话框传递给函数参数。
public void verificar(View dialogView){
SharedPreferences sharedPref = getSharedPreferences("PREFS",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
EditText b = dialogView.findViewById(R.id.userName);
String nombre = b.getText().toString(); //Obtengo el nombre que se inserto en el cuadro
editor.putInt("lastScore",cont);
editor.putString("lastNombre",nombre);
editor.commit();
Intent intent = new Intent(getApplicationContext(),Ranking.class);
startActivity(intent);
}
如下所示更改您的 deseaFigurar 函数。因为当您调用 findViewById(R.id.userName)
时。它是属于 Activity 的一个方法,用于在 Activity 布局中查找 View ,因此会导致 null 异常,因为没有带有 userName id 的 EditText。您需要在膨胀的对话框 View 中找到它。
public void deseaFigurar(){
final AlertDialog.Builder builder= new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
.setPositiveButton("Figurar" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
verificar(dialogView);
finish();
}
})
.setNegativeButton("No figurar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(MainActivity.this,Ranking.class);
startActivity(intent);
}
});
builder.show();
}
关于java - 带有 EditText 字符串的 SharedPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56613971/