我正在开发一个 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