2015-08-17 31 views
0

我有一個類繼承的AsyncTask接收背景下,字符串的用戶和密碼字符串,如何在Android中執行的AsyncTask - 類埃羅Zygotelnit

我嘗試執行我的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); 
     } 
    } 

按照我下面的類,它執行的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) 
+0

「你知道有什麼問題嗎?」你是否應該告訴我們問題是什麼並尋求解決方案? – e4c5

+0

我的問題是:任務不工作......但我不知道爲什麼asynctask不工作。 –

+0

我想你應該讀這第一個http://stackoverflow.com/help/how-to-ask – e4c5

回答

0

new ProgressDialog(context);public LoginService(Context context, String username, String password)之前執行,當progressDialog被初始化,這意味着,該context仍然null,這將導致NPE

刪除ProgressDialog progressDialog = new ProgressDialog(context);public LoginService(Context context, String username, String password)

0

也應該覆蓋onPostExecute。如果還是錯誤,發表您的logcat

UPDATE:

對於progressDialog:

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

你好BKN跟着我的日誌貓 –

+0

我會放在我的問題 –

+0

問題可能在你的progressDialog,我沒有找到你使用它的地方,除了'ProgressDialog progressDialog = new ProgressDialog(context);'。如果不使用,請將其移除。 – BNK

0

全部,非常感謝幫忙initialze progressDialog

我刪除ProgressDialog progressDialog = new ProgressDialog(contex); 並加入班級LoginService

它現在正在工作。

謝謝!

+0

如果我的回答可以幫助你,雖然你不接受,但你可以放棄,不是嗎? :) – BNK

+0

是的...我會upvote ..感謝你非常mich爲您的幫助。 –