我是Android新手。我一直在嘗試編寫一些視圖,事件處理程序和共享偏好類。在下面的代碼中,我得到以下錯誤:空返回錯誤(空指針異常)
「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方法裏面? –
EditText用戶名你已經在本地聲明它可以在onClick訪問嗎? – Raghavendra