菜鸟求助 这么简单一个登陆获取字符串的程序为什么无法运行可通过编译。。。。麻烦看下谢谢了

延陵小明 2013-05-09 05:02:54
我的意思就是自定义一个对话框获得账户 密码以及手机号三种信息并转换为字符串赋值 如下图所示

代码如下
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 kongyeeku@163.com
* @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();


...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
延陵小明 2013-05-09
  • 打赏
  • 举报
回复
引用 5 楼 wlcw16 的回复:
我猜你username=(EditText)findViewById(R.id.username); passward=(EditText)findViewById(R.id.passward); phonenumber=(EditText)findViewById(R.id.phonenumber); 这三个是写在R.layout.login这里面的。 但是你却是在R.layout.main中找他们,所以当然会拿到空值。
的确是这种情况 多谢了前辈
wlcw16 2013-05-09
  • 打赏
  • 举报
回复
试试这么写 username=(EditText)loginForm.findViewById(R.id.username); passward=(EditText)loginForm.findViewById(R.id.passward); phonenumber=(EditText)loginForm.findViewById(R.id.phonenumber); 写在alert builder 里面
wlcw16 2013-05-09
  • 打赏
  • 举报
回复
我猜你username=(EditText)findViewById(R.id.username); passward=(EditText)findViewById(R.id.passward); phonenumber=(EditText)findViewById(R.id.phonenumber); 这三个是写在R.layout.login这里面的。 但是你却是在R.layout.main中找他们,所以当然会拿到空值。
wlcw16 2013-05-09
  • 打赏
  • 举报
回复
引用 3 楼 guoming0000 的回复:
[quote=引用 1 楼 wlcw16 的回复:] 最好把崩溃log发出来,自己也可以先看看log,然后定位错误,分析错误。 估计是空指针错误吧。 加个判断试试 if(null != username.getText()){ String name=username.getText().toString(); } 三个都加。
加了没用 一样报错 日志如下[/quote] 空指针,外层再加非空判断 if(null != username){ if(null != username.getText()){ String name=username.getText().toString(); } }
延陵小明 2013-05-09
  • 打赏
  • 举报
回复
引用 1 楼 wlcw16 的回复:
最好把崩溃log发出来,自己也可以先看看log,然后定位错误,分析错误。
估计是空指针错误吧。
加个判断试试
if(null != username.getText()){
String name=username.getText().toString();
}
三个都加。


加了没用 一样报错 日志如下
veardn 2013-05-09
  • 打赏
  • 举报
回复
有可能是控件为null,看看username=(EditText)findViewById(R.id.username); passward=(EditText)findViewById(R.id.passward); phonenumber=(EditText)findViewById(R.id.phonenumber);这3个都取到没
wlcw16 2013-05-09
  • 打赏
  • 举报
回复
最好把崩溃log发出来,自己也可以先看看log,然后定位错误,分析错误。 估计是空指针错误吧。 加个判断试试 if(null != username.getText()){ String name=username.getText().toString(); } 三个都加。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧