1

如果密码和登录 ID 不匹配,我想提醒对话框显示。我尝试了以下代码,但是当我运行时,如果文本相同,则它会执行,但如果密码和登录 ID 不匹配,则应该显示警报,但进程退出时说不幸的是您的项目是结束了。

我在下面附上了我的代码

package com.example.explicitintent;

import java.security.PublicKey;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button b1, b2,b3;
    EditText e1, e2;
    String username="saras", password="greek";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1 = (EditText) findViewById(R.id.editText0001);
        e2 = (EditText) findViewById(R.id.editText0002);
        b1 = (Button) findViewById(R.id.button0002);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                if (username.equals(e1.getText().toString()) && (password.equals(e2.getText().toString())))
                {
                    Intent b = new Intent(MainActivity.this,Contacts.class);
                    String s = e1.getText().toString();
                    String s1 = e2.getText().toString();
                    b.putExtra("d1", s);
                    b.putExtra("d2", s1);
                    startActivity(b);
                }
                else
                {

                    AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext()); 

                    alt.setIcon(R.drawable.ic_launcher);
                    alt.setTitle("WARNING");
                    alt.setMessage("Do u want to re-enter password");
                    alt.setPositiveButton("YES", new DialogInterface.OnClickListener() 
                    {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1)
                        {
                            Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();

                        }
                    });

                    alt.setNegativeButton("NO",new DialogInterface.OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) 
                        {
                            Toast.makeText(getApplicationContext(),"OK", Toast.LENGTH_SHORT).show();

                        }
                    });
                    alt.show();

                }
                }


        });

        b2 = (Button) findViewById(R.id.button0003);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent c = new Intent(MainActivity.this, Reset.class);
                startActivity(c);
            }
        });

        b3 = (Button) findViewById(R.id.button1);
        b3.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) 
            {
                Toast.makeText(getApplicationContext(),"Password Saved", Toast.LENGTH_LONG).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




}
4

1 回答 1

3

更改此行

 AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext());

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

你应该改成arg0有意义的东西,比如viewor v。如果这不起作用,请发布 logcat,以便我们查看您遇到的错误。您需要根据Context情况使用适当的,在这里您希望AlertDialog使用与您的(或)Activity Context相同的。ContextButtonarg0

注意 MainActivity.this在这里会做同样的事情,argo.getContext()但我最近被告知这是不好的做法,例如如果你想重用这个代码,那么你必须更改代码的活动名称部分。它是一种不太动态的访问Context.

这是一个很好的 SO 答案,可以解决Context. 一开始这可能是一个很难掌握的概念,因此您可能需要仔细阅读它几次并将其放在附近。

于 2013-08-27T17:34:24.507 回答