致命异常:当我使用Soap时asynctask#1 doInBackground

时间:2014-04-28 09:44:40

标签: android

当我运行我的代码时,我得到了这个错误,我不知道为什么! 我认为我在doinbackground方法中的错误,但我不知道原因 我有类SoapConnection这使我的Web服务连接,当我运行我的应用程序并写入正确的用户名和密码新的活动打开正常,但当写错误的用户名或密码时,我显示此错误(致命异常:asynctask#1 doInBackground)

import java.util.HashMap;
import java.util.Map;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Paint;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity {

TextView lblRenewal, lblForgot;
private static EditText txtUser , txtPass;  
Button btnLogin, btnCancel;
ProgressDialog pDialog;

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

    txtUser =(EditText)findViewById(R.id.txtUsername);
    txtPass =(EditText)findViewById(R.id.txtPassword);

    lblRenewal = (TextView) findViewById(R.id.lblRenewal);
    lblRenewal.setClickable(true);
    lblRenewal.setMovementMethod(LinkMovementMethod.getInstance());
    String text = "<a href='http://www.Google.com/' > google </a>";
    lblRenewal.setText(Html.fromHtml(text));

    lblForgot = (TextView) findViewById(R.id.lblForgot);
    lblForgot.setPaintFlags(lblForgot.getPaintFlags()
            | Paint.UNDERLINE_TEXT_FLAG);
    lblForgot.setClickable(true);

    lblForgot.setOnClickListener(lblForget_Click);

    btnCancel = (Button) findViewById(R.id.btnCancel);
    btnLogin = (Button) findViewById(R.id.btnLogin);

    btnCancel.setOnClickListener(btnCancel_Click);
    btnLogin.setOnClickListener(btnLogin_Click);

}

private OnClickListener lblForget_Click = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent PasswordRecovery = new Intent(
                "com.examples.myapp.FORGOTPASSWORD");
        startActivity(PasswordRecovery);
    }
};


private OnClickListener btnCancel_Click = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        finish();
        System.exit(0);
    }
};

private OnClickListener btnLogin_Click = new OnClickListener() {

    @Override
    public void onClick(View v) {

        try{
        AsyncTask<String, Object , Object> LoginTask = new AsyncTask<String, Object, Object>(){


            @Override
            protected void onPostExecute(Object result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                if(pDialog.isShowing())
                    {
                    pDialog.dismiss();
                    }

            }


            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                pDialog = new ProgressDialog(Login.this);
                pDialog.setMessage("Loading...");
                pDialog.setCancelable(true);
                pDialog.show();

            }

            @Override
            protected String doInBackground(String... arg0) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("User", txtUser.getText());
                params.put("Pass", txtPass.getText());
                SoapConnection SC = new SoapConnection("UserExists", "http://10.0.2.2:38176/Android/BCWS.asmx?WSDL");
                SC.params = params;
                String Result =SC.StartConnection(); 
                if(Result.equals(txtUser.getText().toString())){
                        Intent m = new Intent(Login.this,MainActivity.class);
                        startActivity(m);
                }else{
                Toast.makeText(Login.this, Result, Toast.LENGTH_LONG).show();
                }
                return Result;
            }
        };

        LoginTask.execute();
        }catch( Exception e ){
        txtUser.setText("Error");
        }

    }
};

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

@Override
public void onDestroy() {
    super.onDestroy();
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}


enter code here

3 个答案:

答案 0 :(得分:0)

doInBackground中你正在更新ui,这是不可能的。你需要在ui线程上更新ui。在后台线程

上调用doInBackground

onPostExecute显示ToaststartActivity

答案 1 :(得分:0)

Map<String, Object> params = new HashMap<String, Object>();
            params.put("User", txtUser.getText());
            params.put("Pass", txtPass.getText());
            SoapConnection SC = new SoapConnection("UserExists",     "http://10.0.2.2:38176/Android/BCWS.asmx?WSDL");
            SC.params = params;
            String Result =SC.StartConnection(); 
            if(Result.equals(txtUser.getText().toString())){
                    Intent m = new Intent(Login.this,MainActivity.class);
                    startActivity(m);
            }else{
            Toast.makeText(Login.this, Result, Toast.LENGTH_LONG).show();//this is your error, 
            }
            return Result;

你无法更新doInBackground中的UI你应该在onPostExecute方法中调用Toast,只需执行以下操作。

@Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if(pDialog.isShowing())
                {
                pDialog.dismiss();
                }
            if(Result.equals(txtUser.getText().toString())){
                    Intent m = new Intent(Login.this,MainActivity.class);
                    startActivity(m);
            }else{
            Toast.makeText(Login.this, Result, Toast.LENGTH_LONG).show();//this is your error, 
            } 

        }

答案 2 :(得分:0)

Toast.makeText(Login.this, Result, Toast.LENGTH_LONG).show();

无法在doInBackground中运行。将它移动到onPostExecute或用runOnUiThread换行,如下所示:

        @Override
        protected String doInBackground(String... arg0) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("User", txtUser.getText());
            params.put("Pass", txtPass.getText());
            SoapConnection SC = new SoapConnection("UserExists", "http://10.0.2.2:38176/Android/BCWS.asmx?WSDL");
            SC.params = params;
            String Result =SC.StartConnection(); 
            if(Result.equals(txtUser.getText().toString())){
                    Intent m = new Intent(Login.this,MainActivity.class);
                    startActivity(m);
            }else{
              Login.this.runOnUiThread(new Runnable() {

                  public void run() {
                   Toast.makeText(Login.this, Result, Toast.LENGTH_LONG).show();

              }
           }
            return Result;
        }
     });