警告对话框积极的按钮问题 [英] Alert Dialog Positive Button Issue

查看:192
本文介绍了警告对话框积极的按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始警告对话框上点击按钮,它有用户名和密码后,警告对话框解雇,如果你preSS OK按钮,它强制关闭,关闭应用程序,

I have alert dialog initiated on button click , it had username and password , after alert dialog fired , if you press OK button , it force close and close the app ,

任何帮助将AP preciated。

any help will be appreciated.

 public class MainActivity extends Activity {
final Context context = this;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);  

    TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
    tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));

    TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
    tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
    tv1.setText(Html.fromHtml(getString(R.string.introclusion)));



button = (Button) findViewById(R.id.button1);

// add button listener
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        LayoutInflater li = LayoutInflater.from(context);
        View dialog_layoutView = li.inflate(R.layout.dialog_layout, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setView(dialog_layoutView);
        alertDialogBuilder

                .setCancelable(false)
                .setPositiveButton("OK",

                        new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {

                                EditText username = (EditText) findViewById(R.id.txt_name);
                                EditText password = (EditText) findViewById(R.id.password);

                                if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
                                    if(username.getText().toString().equals("test") && password.getText().toString().equals("test")) {
                                Intent intent = new Intent(MainActivity.this,Text.class);
                                startActivity(intent);
                                finish();}
                                    else{
                                        // get your custom_toast.xml layout
                                        LayoutInflater inflater = getLayoutInflater();

                                        View layout = inflater.inflate(R.layout.custom_toast,
                                                (ViewGroup) findViewById(R.id.custom_toast));

                                        // set a dummy image
                                        ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
                                        image.setImageResource(R.drawable.ic_launcher);

                                        // set a message
                                        TextView text = (TextView) layout.findViewById(R.id.text_toast);
                                        text.setText("Wrong username or password");

                                        // Toast...
                                        Toast toast = new Toast(getApplicationContext());
                                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                                        toast.setDuration(Toast.LENGTH_LONG);
                                        toast.setView(layout);
                                        toast.show();}}}    


                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

    }

});

  }

  }

logcat的:

LOGCAT:

  java.lang.NullPointerException
at com.example.demo.MainActivity$1$1.onClick(MainActivity.java:63)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)

dialog_layout.xml:

dialog_layout.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="fill_parent"
android:orientation="vertical"
android:padding="10sp" >

<EditText
    android:id="@+id/txt_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/dialog_uname"
    android:singleLine="true" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" >
</EditText>

推荐答案

修改

LayoutInflater li = LayoutInflater.from(context);

LayoutInflater li = LayoutInflater.from(arg0.getContext());

和变化

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(arg0.getContext());

背景变量,因为你的活动之前初始化创建。而你并不需要创建一个背景活动的内部变量。如果出于某种原因(也许它的简单),你要使用的变量则声明它你在哪里,但它初始化内部的onCreate()

Your context variable is null because you initialize it before the Activity is created. And you don't need to create a context variable inside of an Activity. If, for whatever reason (maybe its simplicity) you want to use the variable then declare it where you are but initialize it inside of onCreate()

此外,里面的onClick()签名,我会在变量名更改为有意义但是这不是必需的。像chanig

Also, inside the onClick() signature, I would change the variable name to something meaningful but that's not required. Like chanig

public void onClick(View arg0)

public void onClick(View v)

修改

您还需要更改这些行

EditText username = (EditText) findViewById(R.id.txt_name);
EditText password = (EditText) findViewById(R.id.password);

EditText username = (EditText) dialog_layoutView.findViewById(R.id.txt_name);
EditText password = (EditText) dialog_layoutView.findViewById(R.id.password);

这会告诉他们在 dialog_layout 的样子。目前,他们正在寻找在布局你用膨胀的setContentView()

This will tell them to look in dialog_layout. Currently they are looking in the layout that you inflated with setContentView()

这篇关于警告对话框积极的按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆