2017-12-18 24 views
-1

私はAndroidが初めてです。私は、いくつかのビュー、イベントハンドラ、および共有プリファレンスクラスのコーディングを試してきました。以下のコードでは、私は、次のエラーを取得する:nullリターンエラー(nullポインタ例外)

「java.lang.NullPointerExceptionが:NULLオブジェクト参照に仮想メソッドを呼び出すための試み 『android.text.Editable android.widget.EditText.getText()』」

何度も自分のコードをチェックしましたが、nullオブジェクトを呼び出すコードを見つけることができません。助けてください。

メインクラス:

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レイアウト:

EditText username = (EditText) findViewById(R.id.editText); 
EditText password = (EditText) findViewById(R.id.editText2); 

テイクを:

<?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 username uはローカルで宣言していますか?onClickでアクセスできますか? – Raghavendra

答えて

0

あなたonClick(View view)方法は、あなたが宣言したあなたのonCreate()メソッド変数にアクセスすることはできません。それらをメソッドから外し、クラス内で宣言します。

単にあなたのコードは次のようになります。

--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

この回答が役に立ちましたら、それをアップアップして「正解」と記入してください –