Android1.6:"android.ます。WindowManager$BadTokenException:できない追加ウィンドウトークンがnullの場合はアプリ"
-
26-09-2019 - |
質問
私は開こうとしているダイアログウィンドウが毎回っているのですこの例外:
Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException:
Unable to add window -- token null is not for an application
at android.view.ViewRoot.setView(ViewRoot.java:460)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.app.Dialog.show(Dialog.java:238)
at android.app.Activity.showDialog(Activity.java:2413)
私の創造を呼び出します showDialog
のディスプレイのidです。の onCreateDialog
ハンドラグスプリングフィーステップ実行することができれない問題ですが、私は付いてない何かが足りない:
@Override
public Dialog onCreateDialog(int id)
{
Dialog dialog;
Context appContext = this.getApplicationContext();
switch(id)
{
case RENAME_DIALOG_ID:
Log.i("Edit", "Creating rename dialog...");
dialog = new Dialog(appContext);
dialog.setContentView(R.layout.rename);
dialog.setTitle("Rename " + noteName);
break;
default:
dialog = null;
break;
}
return dialog;
}
のようなものはありま欠から。あいつ、この問題作成時にダイアログから onCreate
, がでの活動なたはまだこれからの通話メニューからオブジェクトの appContext
変数が感じられるようにすることが可能ですが人口のアンインストールします。
解決
の代わりに:
Context appContext = this.getApplicationContext();
あなたは(おそらくthis
)にいる活動へのポインタを使用する必要があります。
私はあまりにも、この今日にかまれてしまった、迷惑な部分がgetApplicationContext()
あるdeveloper.android.comから逐語的である:(
他のヒント
あなたは活動ではないコンテキストを介してアプリケーションウィンドウ/ダイアログを表示することはできません。有効なアクティビティの参照
を渡して試してみてくださいgetApplicationContextの事に同上ます。
アンドロイドサイト上の文書には、それを使用すると言うが、それは動作しません... grrrrr :-P
ジャスト行います:
dialog = new Dialog(this);
を使用すると、ダイアログを起動し、そこから "この" のですが、通常、あなたのアクティビティます。
AndroidのドキュメントgetApplicationContext()を使用することが提案されている。
AlertDialog.BuilderまたはAlertDialogかをインスタンス化中にが、それはその使用するのではなく、あなたの現在の活動を動作しません]ダイアログ...
例:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
または
AlertDialog.Builder builder = new AlertDialog.Builder((Your Activity).this);
その代わりgetApplicationContext()
の、ちょうど使用ActivityName.this
また、同様の問題があったかクラスのようなこと:
public class Something {
MyActivity myActivity;
public Something(MyActivity myActivity) {
this.myActivity=myActivity;
}
public void someMethod() {
.
.
AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
.
AlertDialog alert = builder.create();
alert.show();
}
}
現代美術館などがほとんどの時間をもうけられることがありま墜落同じエラーになります。その実現に MyActivity
あったんです...
public class MyActivity extends Activity {
public static Something something;
public void someMethod() {
if (something==null) {
something=new Something(this);
}
}
}
ったので、オブジェクトとして static
, は、次のコードでしたが、オリジナル版のオブジェクトは、このようにしたものを参考に独自の Activity
, で、長く存在しています。
愚かの間違い、まったく気付きません必要なのオブジェクトとして static
最初の場---
だけにそれを変更する
AlertDialog.Builder alert_Categoryitem =
new AlertDialog.Builder(YourActivity.this);
の代わりに
AlertDialog.Builder alert_Categoryitem =
new AlertDialog.Builder(getApplicationContext());
別の解決策は、システムダイアログにウィンドウの種類を設定することです。
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
これは SYSTEM_ALERT_WINDOW
の許可を必要とします:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
ドキュメントが言うように:
非常に少数のアプリケーションでは、この権限を使用する必要があります。これらのウィンドウは、ユーザとシステムレベルの相互作用を目的としています。
これはあなたがアクティビティに接続されていないのダイアログが必要な場合にのみ使用すべきソリューションです。
dialouge
を宣言するにgetApplicationContext()
を使用しないでください
必ず利用this
たりactivity.this
これは私のために働いた -
new AlertDialog.Builder(MainActivity.this)
.setMessage(Html.fromHtml("<b><i><u>Spread Knowledge Unto The Last</u></i></b>"))
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).show();
使用
ActivityName.this
は、この問題は非常に一般的であるネストされたダイアログでは、それが動作するとき、
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
が代わりに使用されている
mDialogBuilder = new AlertDialog.Builder(getApplicationContext);
この代替ます。
また、これを行うことができます。
public class Example extends Activity {
final Context context = this;
final Dialog dialog = new Dialog(context);
}
これは私のために働きました!
それは言われていますと、あなたはダイアログのコンテキストとしての活動を必要とし、使用「YourActivity.this」静的コンテキストのか、ここでをチェックセーフモード
でダイナミックなものを使用する方法については、こちらタグに
dialog
ウィンドウの種類をリセットしてみてください
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
許可android.permission.SYSTEM_ALERT_WINDOW
public class Splash extends Activity {
Location location;
LocationManager locationManager;
LocationListener locationlistener;
ImageView image_view;
ublic static ProgressDialog progressdialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressdialog = new ProgressDialog(Splash.this);
image_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
locationManager.requestLocationUpdates("gps", 100000, 1, locationlistener);
Toast.makeText(getApplicationContext(), "Getting Location plz wait...", Toast.LENGTH_SHORT).show();
progressdialog.setMessage("getting Location");
progressdialog.show();
Intent intent = new Intent(Splash.this,Show_LatLng.class);
// }
});
}
ここにテキスト: -
activity
ためprogressdialog
コンテキストを取得するためにこれを使用
progressdialog = new ProgressDialog(Splash.this);
またはprogressdialog = new ProgressDialog(this);
BroadcastListener
のためのアプリケーション・コンテキストを取得するためにこれを使用
ないprogressdialog
ためます。
progressdialog = new ProgressDialog(getApplicationContext());
progressdialog = new ProgressDialog(getBaseContext());
最高とメモリリークの問題を回避、AsyncTaskの「ProgressDialog」を表示する最も安全な方法はLooper.mainと「ハンドラを」()を使用することです。
private ProgressDialog tProgressDialog;
し、 'のonCreate'
で tProgressDialog = new ProgressDialog(this);
tProgressDialog.setMessage(getString(R.string.loading));
tProgressDialog.setIndeterminate(true);
これは、設定部で行わrを。今AsyncTaskで 'showProgress()' と 'hideProgress()' を呼び出します。
private void showProgress(){
new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
tProgressDialog.show();
}
}.sendEmptyMessage(1);
}
private void hideProgress(){
new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
tProgressDialog.dismiss();
}
}.sendEmptyMessage(1);
}