空返回错误(空指针异常)
我是Android新手。我一直在尝试编写一些视图,事件处理程序和共享偏好类。在下面的代码中,我得到以下错误:空返回错误(异常" title="空指针异常">空指针异常)
“java.lang.NullPointerException:试图调用虚拟方法'android.text.Editable android.widget.EditText.getText()'对空对象引用”
我检查了我的代码很多次,但我找不到代码,我要调用一个空对象。请帮忙。
主要类:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
EditText username = (EditText) findViewById(R.id.editText);
EditText password = (EditText) findViewById(R.id.editText2);
b.setOnClickListener(this);
TextView displayText = findViewById(R.id.textView);
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
if(storedUsername == null){
storedUsername = pref.getString("username", "empty");
}
if (storedPassword == null){
storedPassword = pref.getString("password", "empty");
}
displayText.setText(storedUsername + " " + storedPassword);
}
@Override
public void onClick(View view) {
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.czolb.persistence.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="148dp"
android:layout_marginStart="148dp"
android:layout_marginTop="38dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="84dp"
android:layout_marginStart="85dp"
android:layout_marginTop="73dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="84dp"
android:layout_marginStart="85dp"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="163dp"
android:layout_marginStart="163dp"
android:layout_marginTop="60dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>
回答:
你onClick(View view)
方法不必你声明的onCreate()
方法变量访问:
EditText username = (EditText) findViewById(R.id.editText); EditText password = (EditText) findViewById(R.id.editText2);
取他们脱离方法并在你的课堂内宣布。
只需将您的代码应该是这样的:
--YourClassName-- { EditText username;
EditText password;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button);
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
b.setOnClickListener(this);
TextView displayText = findViewById(R.id.textView);
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
if(storedUsername == null){
storedUsername = pref.getString("username", "empty");
}
if (storedPassword == null){
storedPassword = pref.getString("password", "empty");
}
displayText.setText(storedUsername + " " + storedPassword);
}
@Override
public void onClick(View view) {
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
editor.commit();
}
}
以上是 空返回错误(空指针异常) 的全部内容, 来源链接: utcz.com/qa/263802.html