我的意思就是自定义一个对话框获得账户 密码以及手机号三种信息并转换为字符串赋值 如下图所示代码如下
package org.crazyit.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class LoginDialog extends Activity
{
EditText username;
EditText passward;
EditText phonenumber;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bn = (Button)findViewById(R.id.bn);
username=(EditText)findViewById(R.id.username);
passward=(EditText)findViewById(R.id.passward);
phonenumber=(EditText)findViewById(R.id.phonenumber);
//定义一个AlertDialog.Builder对象
final Builder builder = new AlertDialog.Builder(this);
//为按钮绑定事件监听器
bn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View source)
{
// 设置对话框的图标
builder.setIcon(R.drawable.tools);
// 设置对话框的标题
builder.setTitle("自定义普通对话框");
//装载/res/layout/login.xml界面布局
TableLayout loginForm = (TableLayout)getLayoutInflater()
.inflate( R.layout.login, null);
// 设置对话框显示的View对象
builder.setView(loginForm);
// 为对话框设置一个“确定”按钮
builder.setPositiveButton("登录"
// 为按钮设置监听器
, new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
String name=username.getText().toString();
String pass=passward.getText().toString();
String num=phonenumber.getText().toString();
}
});
// 为对话框设置一个“取消”按钮
builder.setNegativeButton("取消"
,  new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//取消登录,不做任何事情。
}
});
//创建、并显示对话框
builder.create().show();
}
});
}
}只要点击登录就会崩溃 应该是这三句话有问题 但是不明白为什么错 把这三句话删了就没事了。。
String name=username.getText().toString();
String pass=passward.getText().toString();
String num=phonenumber.getText().toString();

解决方案 »

  1.   

    最好把崩溃log发出来,自己也可以先看看log,然后定位错误,分析错误。
    估计是空指针错误吧。
    加个判断试试
    if(null != username.getText()){
    String name=username.getText().toString();
    }
    三个都加。
      

  2.   

    有可能是控件为null,看看username=(EditText)findViewById(R.id.username);
            passward=(EditText)findViewById(R.id.passward);
            phonenumber=(EditText)findViewById(R.id.phonenumber);这3个都取到没
      

  3.   


    加了没用 一样报错 日志如下空指针,外层再加非空判断
    if(null != username){
    if(null != username.getText()){
    String name=username.getText().toString();
    }
    }
      

  4.   

    我猜你username=(EditText)findViewById(R.id.username);
            passward=(EditText)findViewById(R.id.passward);
            phonenumber=(EditText)findViewById(R.id.phonenumber);
    这三个是写在R.layout.login这里面的。
    但是你却是在R.layout.main中找他们,所以当然会拿到空值。
      

  5.   

    试试这么写
    username=(EditText)loginForm.findViewById(R.id.username);
    passward=(EditText)loginForm.findViewById(R.id.passward);
    phonenumber=(EditText)loginForm.findViewById(R.id.phonenumber);写在alert builder 里面