无法从Android中的EditText Control读取文本

时间:2012-11-06 12:08:04

标签: android layout nullpointerexception android-edittext

我有一个使用自定义布局的AlertDialog。在OnClick事件中,我想阅读用户键入的内容,所以我这样做:

//Login Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // Get the layout inflater
            LayoutInflater inflater = this.getLayoutInflater();

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.dialog_signin, null))
            .setPositiveButton(R.string.tvLoginTitle, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       String username = ((EditText)findViewById(R.id.loginUsername)).getText().toString();
                       String password = ((EditText)findViewById(R.id.loginPassword)).getText().toString();
                       System.out.println(username);
                       System.out.println(password);
                       ConnectionID.loginToServer(cMainActivity, username, password);
                   }
               })
               .setNegativeButton(R.string.btnExit, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       System.exit(0);

                   }
               }).show();  

但是当我想阅读里面的内容时,我总是得到一个NullPointerException,来自dialog_signin的布局如下:

<EditText
    android:id="@+id/loginUsername"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="@string/tvUsername" />
<EditText
    android:id="@+id/loginPassword"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="@string/tvPassword"/>

3 个答案:

答案 0 :(得分:0)

如果用户名和密码是属于对话框的字段,则应按以下代码所示进行操作:

编辑:

View v = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(v); 
String username = ((EditText)v.findViewById(R.id.loginUsername)).getText().toString();       
String password = ((EditText)v.findViewById(R.id.loginPassword)).getText().toString();

答案 1 :(得分:0)

使用以下内容:

View v=inflater.inflate(R.layout.dialog_signin, null);

并替换:

String username = ((EditText)findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)findViewById(R.id.loginPassword)).getText().toString();

String username = ((EditText)v.findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)v.findViewById(R.id.loginPassword)).getText().toString();

答案 2 :(得分:0)

与Dialog一起使用,如下所示,对我来说很有用,也很有用,

Dialog builder = new Dialog(this);
builder.setContentView(R.layout.dialog_signin);


String username = ((EditText)builder.findViewById(R.id.loginUsername)).getText().toString();
String password = ((EditText)builder.findViewById(R.id.loginPassword)).getText().toString();

//Your buttons

builder.show();