0

我正在开发一个 Android 项目,因为我需要将登录数据发送到在线数据库。
对于该 asynctask 类被使用。

但我在 logcat 中遇到了内部异常并强制关闭。

这是我的代码

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
    EditText un,pw;
    Button ok;
    String res;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        un=(EditText)findViewById(R.id.u_name);
        pw=(EditText)findViewById(R.id.passwd);
        ok=(Button)findViewById(R.id.btnlogin);

        ok.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
                // TODO Auto-generated method stub
                GetData task = new GetData();
                task.execute();


            }
        });
    }

    private class GetData extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            //String valid = "1";
            String response = null;
                try {
                    response = CustomHttpClient.executeHttpPost("http://127.0.0.1/test_login/check.php", postParameters);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                res=response.toString();
               // res = res.trim();
                res= res.replaceAll("\\s+","");                              
                //error.setText(res);
            return response;
        }

        protected void onPostExecute(String result)
        {
        result = res;
               if(result.equals("1"))
               {
                   Toast toast = Toast.makeText(LoginActivity.this, 
                             "LoginSuccess", Toast.LENGTH_LONG);
                   toast.show();
                   Intent n = new Intent(getApplicationContext(),TestActivity.class);
                   startActivity(n);
               }
                else
                {
                    Toast toast = Toast.makeText(LoginActivity.this, 
                         "Login failed", Toast.LENGTH_LONG);
                    toast.show();
                }

        }
    }
}

我收到以下 logcat 错误

10-11 23:43:10.819: E/AndroidRuntime(1335): FATAL EXCEPTION: AsyncTask #1
10-11 23:43:10.819: E/AndroidRuntime(1335): java.lang.RuntimeException: An error occured while executing doInBackground()
10-11 23:43:10.819: E/AndroidRuntime(1335):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.lang.Thread.run(Thread.java:856)
10-11 23:43:10.819: E/AndroidRuntime(1335): Caused by: java.lang.NullPointerException
10-11 23:43:10.819: E/AndroidRuntime(1335):     at com.oranz.epark.LoginActivity$GetData.doInBackground(LoginActivity.java:55)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at com.oranz.epark.LoginActivity$GetData.doInBackground(LoginActivity.java:1)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-11 23:43:10.819: E/AndroidRuntime(1335):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-11 23:43:10.819: E/AndroidRuntime(1335):     ... 5 more
4

2 回答 2

2

问题在于您的响应字符串为空。

即使在执行 try catch 之后,您也强制运行代码。因此,如果 executeHttpPost 方法遇到某种问题,您的响应字符串将为空,并且在您正在执行的下一行中,

 res=response.toString();

其中repsonse实际上是空的并且达到了NPE。

编辑

像这样改变你的try catch,

        try {
            response = CustomHttpClient.executeHttpPost("http://127.0.0.1/test_login/check.php", postParameters);
        } catch (Exception e) {
            return null;
            e.printStackTrace();
        }

并执行后,

protected void onPostExecute(String result)
        {
        result = res;
                if(result==null)
                {
  Toast toast = Toast.makeText(LoginActivity.this, 
                             "Login Failed. Try again", Toast.LENGTH_LONG);
                   toast.show();
                }

             else  if(result.equals("1"))
               {
                   Toast toast = Toast.makeText(LoginActivity.this, 
                             "LoginSuccess", Toast.LENGTH_LONG);
                   toast.show();
                   Intent n = new Intent(getApplicationContext(),TestActivity.class);
                   startActivity(n);
               }
                else
                {
                    Toast toast = Toast.makeText(LoginActivity.this, 
                         "Login failed", Toast.LENGTH_LONG);
                    toast.show();
                }

        }
于 2012-10-12T08:15:10.773 回答
1

和AsyncTask无关,再看日志。

     10-11 23:43:10.819: E/AndroidRuntime(1335): Caused by: java.lang.NullPointerException
     10-11 23:43:10.819: E/AndroidRuntime(1335):     at com.oranz.epark.LoginActivity$GetData.doInBackground(LoginActivity.java:55)

第 55 行的空指针异常,它位于 doInBackground 中的某处。打开调试器,一行一行地检查哪里是空的。

如果我不得不猜测,我会猜测您的 try/catch 正在引发异常并且您的响应为空。

于 2012-10-12T08:12:38.977 回答