简体   繁体   English

调用AsyncTask Android时崩溃

[英]Crash when calling AsyncTask Android

I have one wcf service that returns true or false depending on the input (to check user log in). 我有一个wcf服务,根据输入(检查用户登录)返回true或false。 I'm trying to implement that in android app but whenever I press the button, the application crashes. 我正在尝试在android应用中实现该功能,但是每当我按下按钮时,应用就会崩溃。 This is the code: 这是代码:

btnLogin.setOnClickListener(new OnClickListener() {

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

            email = editEmail.getText().toString();
            pass = editPass.getText().toString();

            if(email.matches("") || pass.matches("")){
                txtresult.setText("Please fill in the information!");
                return;
            }


            if(!isConnected){
                txtresult.setText("You don't have internet connection!");
                return;
            }

            new MyTask().execute();

            if(status.equals("true")){
                //savePreferences();
                finish();
            }
            else{
                txtresult.setText("Wrong login information!");
            }
        }
    });

And the AsyncTask: 和AsyncTask:

public class MyTask extends AsyncTask<Void,Void,Void> {

    String email1 = editEmail.getText().toString();
    String pass1 = editPass.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://wswob.somee.com/wobservice.svc/checkLogin/"+email1+"/"+pass1);
        HttpContext localContext = new BasicHttpContext();

        HttpResponse response;

          try {

            response = httpclient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            status = EntityUtils.toString(entity);



          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

        return null;
    }
}

That worked normal and suddenly stopped working… I'm not really sure how to debug due to my 2 weeks experience. 那正常,突然停止工作了……由于我有2周的经验,我不确定如何调试。 Any ideas? 有任何想法吗?

this line here: 这条线在这里:

if(status.equals("true")){

is crashing because status hasn't been assigned yet, throwing a NullPointerException. 由于尚未分配status而崩溃,抛出NullPointerException。

AsyncTasks are, by definition, asynchronous. 根据定义,AsyncTask是异步的。 This means that once you make the async call, the code control will return immediately and start the async work in another thread. 这意味着一旦您进行异步调用,代码控件将立即返回并在另一个线程中开始异步工作。

The correct way to do this is to use the AsyncTask callback onPostExecute() , and check your status there instead. 正确的方法是使用AsyncTask回调onPostExecute() ,并在那里检查您的状态。

public class MyTask extends AsyncTask<Void,Void,Void> {

    String email1 = editEmail.getText().toString();
    String pass1 = editPass.getText().toString();

    @Override
    protected Void doInBackground(Void... params) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://wswob.somee.com/wobservice.svc/checkLogin/"+email1+"/"+pass1);
        HttpContext localContext = new BasicHttpContext();

        HttpResponse response;

          try {

            response = httpclient.execute(httpGet, localContext);
            HttpEntity entity = response.getEntity();
            status = EntityUtils.toString(entity);



          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

        return null;
    }

    @Override
    protected Void onPostExecute(Void result){
        super.onPostExecute(result);
        //TODO: your code here. check your status and handle from there
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM