I have a Class extends AsyncTask that receive a Context, String user and String password,
I try execute my AsyncTask, but not working.
When I execute asyncTask class, occurs a problem in Class 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);
}
}
Follow below my Class that execute 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();
Follow below my Class 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;
}
}
Follow the error in log 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)
You should override onPostExecute
also. If still error, post your logcat
UPDATE:
For progressDialog:
if ((progressDialog == null) || !progressDialog.isShowing()){
progressDialog = ProgressDialog.show(this, "TITLE", "MESSAGE");
}
new ProgressDialog(context);
will be executed before public LoginService(Context context, String username, String password)
, that means when progressDialog
is initialized, the context
is still null
, that will cause NPE
.
Remove ProgressDialog progressDialog = new ProgressDialog(context);
or initialze progressDialog
in public LoginService(Context context, String username, String password)
All, thank very much for help.
I remove ProgressDialog progressDialog = new ProgressDialog(contex); and add in construtor in class LoginService
it's working now.
thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.