当前位置:编程问答 > wap >

android对话框点击清除按钮后,出现错误,请大神指导一二

imgcom.yarin.android.Examples_04_14;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Activity01 extends Activity 
{
private ProgressDialog m_Dialog;
private EditText username;
private EditText password;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Dialog dialog = new AlertDialog.Builder(Activity01.this)
.setTitle("登陆提示")//设置标题
.setMessage("这里需要登录!")//设置内容
.setPositiveButton("确定",//设置确定按钮
new DialogInterface.OnClickListener() 
{
public void onClick(DialogInterface dialog, int whichButton)
{
//点击“确定”转向登陆框

LayoutInflater factory = LayoutInflater.from(Activity01.this);
//得到自定义对话框
                final View DialogView = factory.inflate(R.layout.dialog, null);
                //创建对话框
                AlertDialog dlg = new AlertDialog.Builder(Activity01.this)
                .setTitle("登录框")
                .setView(DialogView)//设置自定义对话框的样式
                .setPositiveButton("确定", //设置"确定"按钮
                new DialogInterface.OnClickListener() //设置事件监听
                {
                    public void onClick(DialogInterface dialog, int whichButton) 
                    {
                     //输入完成后,点击“确定”开始登陆
                     m_Dialog = ProgressDialog.show
                                   (
                                  Activity01.this,
                                     "请等待...",
                                     "正在为你登录...", 
                                     true
                                   );
                        
                        new Thread()
                        { 
                          public void run()
                          { 
                            try
                            { 
                              sleep(3000);
                            }
                            catch (Exception e)
                            {
                              e.printStackTrace();
                            }
                            finally
                            {
                             //登录结束,取消m_Dialog对话框
                             m_Dialog.dismiss();
                            }
                          }
                        }.start(); 
                    }
                })
                .setNegativeButton("取消", //设置“取消”按钮
                new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                     //点击"取消"按钮之后退出程序
                     Activity01.this.finish();
                    }
                    
                }).setNeutralButton("清除", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                     //点击"清除"按钮之后清空账号密码
   
                     username=(EditText) findViewById(R.id.username);
                 password=(EditText) findViewById(R.id.password);
                     username.setText(" ");
                     password.setText(" ");
                    }
                })
                .create();//创建
                dlg.show();//显示
}
}).setNeutralButton("退出", 
new DialogInterface.OnClickListener() 
{
public void onClick(DialogInterface dialog, int whichButton)
{
//点击"退出"按钮之后推出程序
Activity01.this.finish();
}
}).create();//创建按钮

// 显示对话框
dialog.show();
}
}
dialog.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/username"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="账号"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />
            
    <EditText
        android:id="@+id/username"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="密码"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />
            
    <EditText
        android:id="@+id/password"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout>
出错截图如下:

点击“清除”后:
--------------------编程问答-------------------- 你的对话框视图是动态加载的,所以应该是(EditText)DialogView.findViewById(R.id.username);
--------------------编程问答-------------------- 直接findViewById是找不到的,要在DialogView里去findViewById,你的EditText是在DialogView里 --------------------编程问答-------------------- 我也遇到了一个一样的错,但不知道原因
--------------------编程问答--------------------   username=(EditText) findViewById(R.id.username);
        password=(EditText) findViewById(R.id.password);
改成:
username=(EditText)DialogView.findViewById(R.id.username);
password=(EditText)DialogView.findViewById(R.id.password); --------------------编程问答-------------------- 下次问问题先打LOG ,ok?
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,