我为我的学校开发了一个应用程序。如果单击“登录”按钮,登录屏幕应显示ProgressDialog。它运行正常,但今天我得到了这个错误(使用Stacktrace)
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at de.eichsfeld.logik.LoginActivity$1LoginTask.onPreExecute(LoginActivity.java:107)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at de.eichsfeld.logik.LoginActivity$1.onClick(LoginActivity.java:193)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我不确定这意味着什么。我谷歌一些similair问题,但新的Progressdialog()中的getApplicationContext()或getParent()不适合我。
所以我希望你能帮助我!
登录异步任务的源代码:
class LoginTask extends AsyncTask< String , String , String >
{
int success;
String uname;
String password;
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Du wirst eingeloggt..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground( String ... args )
{
uname = inputUser.getText().toString();
password = inputPassword.getText().toString();
UserLoginFunctions userFunction = new UserLoginFunctions();
// User wird eingeloggt
JSONObject json = userFunction.loginUser(uname , password);
Log.d("Antwort: " , json.toString());
if (uname.trim().length() > 0
&& password.trim().length() > 0)
{
try
{
// war Login erfolgreich? wenn ja fhre
// weiterfhrende
// Funktionen nach dem Login aus
success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
json_datenbank = json.getString(KEY_DATENBANK);
System.out.println(json_datenbank);
String json_vorname = json
.getString(KEY_VORNAME);
String json_nachname = json
.getString(KEY_NACHNAME);
String json_pushbenachrichtigung = json.getString(KEY_PUSHBENACHRICHTIGUNG);
// speicher alles in einer SharedPreferences
sessionmanager.erzeugeLoginSession(
json_datenbank , json_nachname ,
json_vorname, json_pushbenachrichtigung);
// fhre die registrierung aus
gcmRegistrierung();
// MainFrame anzeigen
Intent mainView = new Intent(
getApplicationContext() ,
FragmentMananger.class);
// Alle anderen Views werden geschlossen
mainView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainView);
// schliesse LoginScreen
finish();
}
}
catch (JSONException e)
{
throw new RuntimeException("Daten konnten nicht geparst werden! " + e.getMessage());
}
}
return null;
}
protected void onPostExecute( String file_url )
{
// schliesse den Progress Dialog
pDialog.dismiss();
if (success == 0)
{
// Logindaten sind falsch
alert.showAlertDialog(
LoginActivity.this ,
"Login fehlgeschlagen" ,
"Bitte Nutzername und Passwort korrekt eingeben" ,
false);
}
}
}
答案 0 :(得分:1)
您可以将上下文传递到异步任务:
LoginTask.class
private Context mcontext;
public LoginTask (Context context) {
// TODO Auto-generated constructor stub
this.mcontext= context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(mcontext.getApplicationContext());
pDialog.setMessage("Du wirst eingeloggt..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
caller.class: -
new LoginTask(getActivity()).execute(params..);