如何在Android中执行AsyncTask - 在类Zygotelnit中的erro

时间:2015-08-17 00:20:46

标签: java android android-asynctask

我有一个Class extends AsyncTask,它接收Context,String用户和String密码,

我尝试执行我的AsyncTask,但没有工作。

当我执行asyncTask类时,在类Zygotelnit

中出现问题
public void run() {
        try {
            mMethod.invoke(null, new Object[] { mArgs });
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        } catch (InvocationTargetException ex) {
            Throwable cause = ex.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else if (cause instanceof Error) {
                throw (Error) cause;
            }
            throw new RuntimeException(ex);
        }
    }

在我的Class下面执行AsyncTask

mButtonEnter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            username = mEditTextUser.getText().toString();
            password = mEditTextPassword.getText().toString();


           new LoginService(LoginActivity.this, username, password).execute();

按照我的AsynTask类下面

public class LoginService extends AsyncTask<String, String, String> {

private String username;
private String password;
private Context context;
ProgressDialog progressDialog = new ProgressDialog(context);

public LoginService(Context context, String username, String password){

    this.context = context;
    this.username = username;
    this.password = password;
}

@Override
protected String doInBackground(String... params) {

    String result = "";

    try {
        URL url = new URL("http://192.168.0.11:8080/appoint-api/api/Usuario/doLogin?user=" + this.username + "&senha=" + this.password);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = bufferedReader.readLine()) != null) {
            response.append(inputLine);
        }

        result = response.toString();
        bufferedReader.close();
    } catch (Exception e) {
        Log.d("InputStream", e.getMessage());
    }

    return result;
  }
}

按照日志Cat

中的错误进行操作
08-16 22:23:29.781  18099-18099/br.com.appoint.android D/AndroidRuntime﹕    Shutting down VM
08-16 22:23:29.781  18099-18099/br.com.appoint.android W/dalvikvm﹕    threadid=1: thread exiting with uncaught exception (group=0x4108b960)
08-16 22:23:29.875  18099-18099/br.com.appoint.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
        at android.app.AlertDialog.<init>(AlertDialog.java:98)
        at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
        at br.com.appoint.android.service.LoginService.<init>(LoginService.java:25)
        at br.com.appoint.android.activity.LoginActivity$1.onClick(LoginActivity.java:63)
        at android.view.View.performClick(View.java:4452)
        at android.widget.Button.performClick(Button.java:148)
        at android.view.View$PerformClick.run(View.java:18428)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

您也应该覆盖onPostExecute。如果仍然错误,请发布您的logcat

更新:

for progressDialog:

if ((progressDialog == null) || !progressDialog.isShowing()){
    progressDialog = ProgressDialog.show(this, "TITLE", "MESSAGE");
}

答案 1 :(得分:0)

new ProgressDialog(context);将在public LoginService(Context context, String username, String password)之前执行,这意味着当progressDialog初始化时,context仍为null,这将导致NPE }。

ProgressDialog progressDialog = new ProgressDialog(context);

中移除progressDialog或初始化public LoginService(Context context, String username, String password)

答案 2 :(得分:0)

所有,非常感谢您的帮助。

我删除ProgressDialog progressDialog = new ProgressDialog(contex); 并在LoginService类中添加construtor

现在正在运作。

谢谢!