对话框不起作用

时间:2014-01-02 08:56:27

标签: java android dialog android-alertdialog

过去3个小时我一直试图实现这个对话框,我无法弄清楚为什么它没有突然出现,我认为最好让全班同学了解我的问题:

Registration.java

public class Registration extends Activity {





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
    }






    //on submit press
    public void SubmitRegistration(View view) {



        // start an asynch request
        class RequestTask extends AsyncTask<String, String, String>{





            @Override
            public String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (IOException e) {
                    //TODO Handle problems..
                }
                return responseString;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //Do anything with response..
            }
        }










           // assign text in fields to string values
           EditText first = (EditText)findViewById(R.id.first);
           String first2 = first.getText().toString();

           EditText last = (EditText)findViewById(R.id.last);
           String last2 = last.getText().toString();

           EditText display = (EditText)findViewById(R.id.display);
           String display2 = display.getText().toString();
           //calculates the number of characters in the display field
           int length2 = display2.length();


           EditText email = (EditText)findViewById(R.id.email);
           String email2 = email.getText().toString();


           EditText password = (EditText)findViewById(R.id.password);
           String password2 = password.getText().toString();


           EditText vpassword = (EditText)findViewById(R.id.vpassword);
           String vpassword2 = vpassword.getText().toString();
           //calculates the number of characters in the password field
           int length = vpassword2.length();









           // verifying the following in order:  Passwords match? A Password field is empty?  
           //Password and Display Name less than 6 characters long? Email contains an @ sign and a period?   
           if(!vpassword2.equals(password2)) {
           Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show(); 


           }
           else if (password2.isEmpty() || vpassword2.isEmpty()){

           Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show(); 
           }
           else if (length < 6 || length2 < 6 ) {
           Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show(); 
           }

           else if (!email2.contains("@") || !email2.contains(".")){
           Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show(); 
           }
           //start else
           else {

           //send php with all the data to server for validation and insertion into table
           String output = null;
                try {
                output = new RequestTask()
                .execute("http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
                .get();
                //example: www.alkouri.com/android/registercheck.php?first=Adam&last=Alkouri&display=arugala&email=arugala@blackbaud.com&password=123
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }





                //if the response from website contains "duplicate" (which means there is a duplicate email address in the DB) then it will display toast.
          if (output.contains("Duplicate")) {
          Toast.makeText(getApplicationContext(), "Email address already in system, press back button if you forgot password and click on Forgot Password ", Toast.LENGTH_LONG).show(); 

          }
          //if the response from website contains "You have registered successfully" then it will send popup message and go to login screen 
          else if (output.contains("You have")){




         //start dialogue
         AlertDialog.Builder alertbox = new AlertDialog.Builder(Registration.this);
         alertbox.setMessage("You have succesfully registered.  Please check your email for further instructions."); // Please Restart Application // "Please restart the app and download your purchase again".
         alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            //create button in dialogue
            public void onClick(DialogInterface arg0, int arg1) 
            {
            //on clicking "ok" in the dialogue box, current activity will close and return to last activity (login screen).
            finish();               

            }
         });
         alertbox.show();
          }     
            }//end else







     }  //end button click task
}//end class

所以,在最底层,直接在最后一个吐司下面,我想要一个对话框弹出。
这是为了让用户知道他们已经注册,他们应该检查他们的电子邮件以获得进一步的指示。

吐司出现得很好,但是当我实施以下任何一个例子时,没有任何东西弹出。
我没有在LogCat中出现任何错误,只是没有任何错误弹出....

I have tried the following examples that I Have seen online:

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

http://stackoverflow.com/questions/2115758/how-to-display-alert-dialog-in-android

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

我做错了什么?

3 个答案:

答案 0 :(得分:1)

AlertDialog.Builder alertbox = new AlertDialog.Builder(MainMenuActivity.this);
        alertbox.setMessage("Please restart the app and download your data again."); // Please Restart Application // "Please restart the app and download your purchase again".
        alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                    //code
            }
        });

        alertbox.setNegativeButton("NO", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //code  
            }
        });

        alertbox.show();

答案 1 :(得分:0)

你需要在onCreate()方法中为你的提交按钮添加一个OnclickListener,并从那里调用你的其他函数。

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //Call your submitregistration function function here                
        }
    });
}

答案 2 :(得分:0)

 output = new RequestTask()
.execute("http://www.mywebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
.get();

调用get不会使其异步。它会阻止ui线程等待结果。

AsyncTask移出onClick

class RequestTask extends AsyncTask<String, String, String>{

http://developer.android.com/reference/android/os/AsyncTask.html#get()

public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

同时将变量声明为实例变量,并在onCreate中初始化您的视图。

EditText first ;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
first = (EditText)findViewById(R.id.first);

在按钮上单击从editText

获取文本

调用asynctask

 new RequestTask().execute(params); // no get

doInbackground计算的结果是onPostExecute的参数。根据结果​​,您可以在onPostExecute

中显示对话框
相关问题