2016-04-25 10 views
-4

EditTextオブジェクトが参照されているときに、onClickメソッドでエラーが発生しています。ここでアンドロイドウィジェットのヌルポインタ例外EditText?

public class messageActivity extends AppCompatActivity { 

    EditText editText; 
    Button button; 
    Logic logic; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_message); 

    editText = (EditText) findViewById(R.id.editText); 
    button = (Button) findViewById(R.id.button6); 

    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      logic.message(editText.getText().toString()); 
     } 
    }); 
    } 
} 

私の活動のEditTextウィジェットのXMLです:

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_below="@+id/textView3" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="83dp" 
     android:inputType="textShortMessage" 
     android:editable="false" /> 
+0

ロジック変数はどこで初期化しますか?これはここではnullポインタのようです。あなたのlogcatを掲示してください –

答えて

1

私はLogicがあなたのクラスのいずれかであることを疑うのですか?変数logicが値で初期化されていないので、NullPointerExceptionを取得しています。logic.messageを実行する前にロジックをインスタンス化する必要があります。

+0

うん、それは...それを見逃してどのように私はそれを見逃していない、そう。助けてくれてありがとう! – thefreebird777

+0

素晴らしい!非常に簡単にこれらのものを見逃す - 私はこれを助けてうれしい、簡単な修正!回答を受け入れて、回答としてマークしてください。 – ishmaelMakitla