android - 警报对话框肯定按钮问题

标签 android android-alertdialog

我在单击按钮时启动了警报对话框,它有用户名和密码,在警报对话框触发后,如果您按“确定”按钮,它会强制关闭并关闭应用程序,

任何帮助将不胜感激。

 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();

    }

});

  }

  }

日志:

  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:

<?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());

您的 context 变量为 null,因为您在创建 Activity 之前对其进行了初始化。而且您不需要在 Activity 内创建 context 变量。如果出于某种原因(也许是为了简单)您想要使用该变量,则在您所在的位置声明它,但在 onCreate()

内部初始化它

此外,在 onClick() 签名中,我会将变量名称更改为有意义的名称,但这不是必需的。喜欢改变

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()

膨胀的 layout

关于android - 警报对话框肯定按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18861082/

相关文章:

android - onStartCommand() 完成后 Service 会自动停止吗?

android - React Native - 如何使用抽屉导航管理 native 后退按钮

php - 没有从 MySQL 检索到 Android ListView 的数据

android - 单击复选框时如何添加确认框?

java - 警报对话框样式不更改多选项目文本颜色

android - AlertDialog 使应用程序崩溃

android - 将 Android 应用程序重新连接到配对的蓝牙设备

android - AlertDialog 多选上的自定义 ListView

Android AlertDialog 按钮无响应

android - xamarin android通知中心到特定设备