0

我试图运行我的应用程序,但是当我在错误 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();
    }

}

}

}
4

2 回答 2

2

NullPointerException在 file 的第 125 行得到一个Login.java。转到您的代码并尝试确定代码中此行中的值如何为空。

我怎么知道这个?

跟踪包含以下代码:

Caused by: java.lang.NullPointerException
...  at com.example.example.Login$AttemptLogin.doInBackground(Login.java:125)
于 2013-08-30T02:24:36.030 回答
0

尝试这个:-

class AttemptLogin extends AsyncTask<String, void, String> 

异步任务使用的三种类型如下:

  1. Params,执行时发送给任务的参数类型。
  2. 进度,在后台计算期间发布的进度单元的类型。
  3. Result,后台计算结果的类型。
于 2013-08-30T04:32:17.903 回答