2017-12-18 113 views
-1

我是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> 
+0

什麼是 '用戶名' 的onClick方法裏面? –

+0

EditText用戶名你已經在本地聲明它可以在onClick訪問嗎? – Raghavendra

回答

0

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(); 

    } 
} 
+0

我編輯了我的答案以顯示代碼。爲什麼不應該編譯?有實例變量和局部變量,他所要做的就是從onCreate方法中取出局部變量並將它們變爲實例變量。 –

+0

謝謝,我現在看到我的錯誤。我很愚蠢。 – czolbe

+0

如果這個答案對您有幫助,請確保將其加註並標記爲「正確答案」 –