我有一个带有登录屏幕的应用程序。检查登录是否正常时,我显示“正在加载” Dialog
。
如果我改变屏幕方向,Dialog
消失并且我认为登录检查再次开始,但之后应用程序崩溃。
我已经阅读了一些解决方案,但我无法做到。
这是我的代码:
MainActivity.java
public void entrar(View view) {
/* Escondemos el teclado */
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextUsuario.getWindowToken(), 0);
/* Comprobamos si hay conexión a Internet */
if(myApplication.isOnline()) {
LoadingMainTask myWebFetch = new LoadingMainTask(this);
myWebFetch.execute();
}
/* Si no se dispone de conexión a Internet, mostramos un error */
else {
myApplication.mostrarMensaje(this, R.string.error_conexion_titulo,
R.string.error_conexion_mensaje);
}
}
private class LoadingMainTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private volatile boolean running = true;
private MainActivity activity;
TuplaUsuario tuplaUsuario = new TuplaUsuario();
private LoadingMainTask(MainActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
dialog.setCancelable(true);
dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cancel(true);
}
});
}
/** application context. */
private Context context;
@Override
protected void onCancelled() {
running = false;
}
@Override
protected void onPreExecute() {
this.dialog.setMessage(getString(R.string.loading));
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
/* Si el login no es correcto, mostramos un error por pantalla */
if (!tuplaUsuario.getOk()) {
myApplication.mostrarMensaje(activity, R.string.error_datos_login_titulo,
tuplaUsuario.getMensaje());
}
/* Entrar */
else {
Intent intent = new Intent(activity, TabsFacturasActivity.class);
startActivity(intent);
}
} else {
System.out.println("throw exception post");
myApplication.throwException(activity);
}
}
@Override
protected Boolean doInBackground(final String... args) {
try{
String usuario = String.valueOf((editTextUsuario).getText());
String password = String.valueOf((editTextPassword).getText());
/* Comprobar datos del login */
try {
tuplaUsuario = myApplication.getDatosUsuario(usuario, password, loginGuardado);
} catch (JSONException e) {
myApplication.throwException(activity);
e.printStackTrace();
} catch (IllegalStateException e) {
myApplication.throwException(activity);
e.printStackTrace();
} catch (IOException e) {
myApplication.throwException(activity);
e.printStackTrace();
}
/* Si el login es correcto, nos guardamos su login, descargamos el resto
* de información y entramos */
if (tuplaUsuario.getOk()) {
boolean rememberMe = myApplication.getRememberMe();
if (rememberMe) {
/* Si el usuario ya estaba guardado, no hace falta volver a guardarlo */
if(!loginGuardado) {
myApplication.guardarUsuario(activity, usuario, password);
}
}
}
return true;
} catch (Exception e){
return false;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/StyledIndicators"
android:configChanges="orientation"
android:name="com.example.factorenergia.MyApplication" >
<activity
android:name="com.example.factorenergia.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
</application>
</manifest>