我试图运行我的应用程序,但是当我在错误 logcat 中执行该过程时收到一条消息“不幸的是应用程序已停止”,出现错误 Fatal Exception Asynctask #1 为什么?对我有什么解决方案吗?
这是一个完整的logcat
08-30 09:15:30.957: E/AndroidRuntime(580): FATAL EXCEPTION: AsyncTask #1
08-30 09:15:30.957: E/AndroidRuntime(580): java.lang.RuntimeException: An error occured while executing doInBackground()
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.lang.Thread.run(Thread.java:856)
08-30 09:15:30.957: E/AndroidRuntime(580): Caused by: java.lang.NullPointerException
08-30 09:15:30.957: E/AndroidRuntime(580): at com.example.example.Login$AttemptLogin.doInBackground(Login.java:125)
08-30 09:15:30.957: E/AndroidRuntime(580): at com.example.example.Login$AttemptLogin.doInBackground(Login.java:1)
08-30 09:15:30.957: E/AndroidRuntime(580): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-30 09:15:30.957: E/AndroidRuntime(580): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-30 09:15:30.957: E/AndroidRuntime(580): ... 5 more
登录.java
package com.example.example;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit, mRegister;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://10.0.2.2/android/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... v) {
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(Login.this, ReadComment.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}