myq_26 2012-11-27 06:12 采纳率: 16.7%
浏览 2894
已采纳

Android - 无法从EditText Control读取文本

程序中有一个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(); 

但是当我想要读出信息的时候,获得一个来自dialog_signin的布局中NullPointerException异常。

<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条回答

  • Baby_Bonnie 2012-11-27 07:38
    关注

    如果用户名和密码是对话框中的,你应该使用以下代码:

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献