AsyncTask类在AlertDialog中没有响应

时间:2015-08-11 06:38:27

标签: android android-asynctask

我正在尝试在AlertDialog内运行登录类,但是没有预期的结果,即使登录类没有logcat错误。希望有人可以帮助我。

public class MainActivity extends Activity 
        //Inflation
        button1= (Button)findViewById(R.id.btnOrderSubmit);
        textView1= = (TextView)findViewById(R.id.editenquiry);

        button1.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
             //Intent method
             }  
        });

        //OnclickListener for TextView
        textView1.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View arg0) {

                AlertDialog.Builder login = new AlertDialog.Builder(MainActivity.this);
                login.setMessage("For Enquiry, Please Login");  

                login.setPositiveButton("Log In", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int arg1) {
                    enquirylogin();
                 }
                 });

                 login.setNegativeButton("Continue", new DialogInterface.OnClickListener(){

                     @Override
                     public void onClick(DialogInterface dialog, int arg1) {
                        //Intent here
                     }
                  });
                login.show();
             }

             private void enquirylogin() {
                // TODO Auto-generated method stub
                LayoutInflater li = LayoutInflater.from(MainActivity.this);
                View promptsView = li.inflate(R.layout.enqlogin, null);

                AlertDialog.Builder enqLogin = new AlertDialog.Builder(MainActivity.this);

                enqLogin.setView(promptsView);

                enusername=(EditText) promptsView.findViewById(R.id.enqusername);
                enpassword=(EditText) promptsView.findViewById(R.id.enqpassword);

                enqLogin.setCancelable(true);
                enqLogin.setMessage("Please Login Now");

                enqLogin.setPositiveButton("Submit",new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                       // TODO Auto-generated method stub
                       new ELogin();
                   }
                }); 

                enqLogin.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                   @Override
                   public void onClick(DialogInterface dialog, int which) { 
                   }
                }); 
                enqLogin.show();
            }

Alertdialog包含提示视图,其中Elogin类正在调用

    class ELogin extends AsyncTask<String, JSONObject, JSONObject>{
                ProgressDialog pDialog;
                protected void onPreExecute() {
                   pDialog = new ProgressDialog(MainActivity.this);
                }
                protected JSONObject doInBackground(String... arg0) {
                    strELogin=enusername.getText().toString();
                    strEPassword=enpassword.getText().toString();

                    //String response =null;
                    List<NameValuePair> params = new ArrayList<NameValuePair>();

                    params.add(new BasicNameValuePair("txtUName", strELogin));
                    params.add(new BasicNameValuePair("txtPass", strEPassword));
                    JSONObject json = jsonParser.makeHttpRequest (LOGIN_URL, "POST", params);

                    return json;
                }
                protected void onPostExecute(JSONObject result) {
                    try {
                    pDialog.dismiss();
                    int success = result.getInt(TAG_SUCCESS);
                    if(success == 1) {
                        Log.d("Login Successful!", result.toString());  
                        Toast.makeText(MainActivity.this, "Authentication Success",Toast.LENGTH_LONG).show();

                        String role_rs = result.getString(TAG_ROLE_RS);
                        String role_id = result.getString(TAG_CID_RS);
                        }else{
                             Toast.makeText(MainActivity.this, "try again!!!!!!", Toast.LENGTH_LONG).show();
                             Log.d("try Again!", result.getString(TAG_MESSAGE)); 
                        }
                }catch (JSONException e) {
                        e.printStackTrace();    
                } catch (NullPointerException e) {
                        e.printStackTrace();
                }
            }
        }               
    });
 }

2 个答案:

答案 0 :(得分:1)

在创建ELogin的新实例时,您应该在其上调用.execute()来启动任务。

官方文档告诉您有关线程和AsyncTasks的所有内容:http://developer.android.com/guide/components/processes-and-threads.html

答案 1 :(得分:0)

缺少以下内容:

new ELogin().execute();

您必须执行() ASYNC 任务。

希望这会对你有所帮助。