2013-12-15 37 views
0

所以我是新來的Android編程,所以我很抱歉如果這是一個初學者的錯誤,但我編程我的按鈕來添加一個用戶到一個數組列表中給出的名稱對話框。當我運行它時,除了執行users.add(新用戶(name))時,所有東西都可以工作。 (它返回一個空pointerI'm不清楚,爲什麼是這種情況。任何幫助將不勝感激。這是我的代碼。Android輸入對話框的Onclick不起作用

addPersonButton.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     final EditText input = new EditText(mainActivity); 

     new AlertDialog.Builder(mainActivity) 
      .setTitle("New User") 
      .setMessage("What is the new user's name?") 
      .setView(input) 
      .setPositiveButton("Add", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        final String name = input.getText().toString(); 
        users.add(new User(name)); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Do nothing. 
       } 
      }).show(); 
    } 
}); 
+0

顯示您的logcat的...... –

+0

其中HVú初始化用戶? –

回答

0

我覺得你的問題是與inputonClick方法,因爲inputdialog layoutmain layout,所以你必須使用下面的代碼:

final String name = dialog.input.getText().toString(); 
    users.add(new User(name)); 
0

在XML創建的EditText和使用findViewById(R.id.edittext);

或Java中

final EditText input = new EditText(MainActivity.this); 

參考here