質問

ないのですが把握、なぜこのことが起こっているのです。このコード:

mProgressDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);

作品だけます。しかし、このコード:

mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);

スローでは、以下の例外:

W/WindowManager(  569): Attempted to add window with non-application token WindowToken{438bee58 token=null}.  Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.access$2100(ActivityThread.java:116)
E/AndroidRuntime( 2049):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
E/AndroidRuntime( 2049):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2049):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 2049):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2049):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2049):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 2049):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 2049):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2049): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049):    at android.view.ViewRoot.setView(ViewRoot.java:460)
E/AndroidRuntime( 2049):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 2049):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 2049):    at android.app.Dialog.show(Dialog.java:238)
E/AndroidRuntime( 2049):    at android.app.ProgressDialog.show(ProgressDialog.java:107)
E/AndroidRuntime( 2049):    at android.app.ProgressDialog.show(ProgressDialog.java:90)
E/AndroidRuntime( 2049):    at com.tastekid.TasteKid.YouTube.onCreate(YouTube.java:45)
E/AndroidRuntime( 2049):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime( 2049):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
E/AndroidRuntime( 2049):    ... 11 more

そのアイデアはなぜことが起こっているか?ねこのら onCreate 方法。

役に立ちましたか?

解決

あなたはどのAPIバージョンを使用していますか?私は右の問題は、これはAndroidの1.6で修正されたされたものについて(APIバージョン4)だ場合ます。

これはgetApplicationContext()がnullにだけポイントを返していることをオブジェクト参照のように見えます。私はあなたが私は窓が実際に構築されて実行される前にonCreate()いくつかのコードが実行されていることに持っていたのと同様の問題を抱えていると思います。 (:300〜400が私のために動作するように見えますが、あなたがいじくり回す必要がありますIIRC)あなたのProgressDialogを開くこと、あなたが必要な何かを開始します(これはハックになるだろうが、数百ミリ秒で新しいスレッドを起動してみています例えばネットワークIO)。このような何かます:

@Override
public void onCreate(Bundle savedInstanceState) {
    // do all your other stuff here

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mProgressDialog = ProgressDialog.show(
               YouTube.this.getApplicationContext(), "",
               YouTube.this.getString(R.string.loading), true);

            // start time consuming background process here
        }
    }, 1000); // starting it in 1 second
}

他のヒント

私はAPIレベルで7本(または類似の)に直面I問題がAndroidのバージョン2.1を使用して、これを使用することによって解決しています

Dialog dialog = new Dialog(this);
これに代えて、

Dialog dialog = new Dialog(getApplicationContext());

希望このことができます:)

私にとって

変更働いていました
builder = new AlertDialog.Builder(getApplicationContext());

builder = new AlertDialog.Builder(ThisActivityClassName.this);

奇妙なことは、最初のものは、Googleのチュートリアルや人々で見つけることができるということである。この上のエラーを取得します..

とは思わないこのタイミングの問題の周りにnullの場合アプリケーションのコンテキスト

を延ばしてみて申請内のアプリは、ただ利用する場合だき,誠にありがとうござい)

public class MyApp extends Application

のインスタンス用として、民間シングルトン.この nullにはならない

private static MyApp appInstance;

く静的ヘルパーにMyApp(利用のシングルトン)

    public static void showProgressDialog( CharSequence title, CharSequence message )
{
    prog = ProgressDialog.show(appInstance, title, message, true); // Never Do This!
}

BOOM!!

また、チェックアウトはandroidエンジニアの回答はこちら WindowManager$BadTokenException

それが原因のひとつでこのエラーが発生する可能性がありするよう表示の応用 ウィンドウのダイアログを通じてコンテキストではない。

今、ないでとってあまり意味がありませんのメソッドはコンテキストparamではなく、活動..

私は自分の状況に問題を修正以下のことがわかった上で答えを読んます。

このエラーを投げた。

myButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
        MyDialogue dialog = new MyDialogue(getApplicationContext());
        dialog.show();              
    }
});

コンテキストが間違ったことを示唆した以前の回答に基づいて、私はビューからのコンテキストを取得するために)(getApplicationContextを変更するには、ボタンのonClickメソッドに渡されます。

myButton.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
        MyDialogue dialog = new MyDialogue(v.getContext());
        dialog.show();              
    }
});

私が間違っている可能性がので、私は完全にジャワの仕組みを理解していないが、私は私の特定の状況の原因は、上記のスニペットは抽象Activityクラスで定義されたという事実に関連している可能性が推測しています。 getApplicationContext()が有効なコンテキストを返さないという事実に貢献したかもしれないことを、継承され、多くの活動で使用されます? (ただの推測)。

私は箇条書きオーバーレイでマップビューを作成しています。私はこのように私のitemizedoverlayを作成していた私のmapActivityから:

OCItemizedOverlay currentLocationOverlay = new OCItemizedOverlay(pin,getApplicationContext);

私はなるだろうことがわかった「android.view.WindowManager $ BadTokenExceptionを:ウィンドウを追加することができません - トークンnullがアプリケーションのためではない」場所がMapViewの上でタップしたときに私itemizedoverlayのONTAP方法は、(トリガされたときに例外)。

私は単純に渡された場合、「これは」の代わりに「getApplicationContext()」の私のコンストラクタに、問題が離れていったことがわかりました。これはalienjazzcatの結論を支持しているようです。奇妙ます。

TabActivities内示す活動のための

のgetParent()

を使用
final AlertDialog.Builder builder = new AlertDialog.Builder(getParent());

の代わりに

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
アンドロイド2.2
については


:このコードを使用します
//activity is an instance of a class which extends android.app.Activity
Dialog dialog = new Dialog(activity);
代わりに、このコードの


// this code produces an ERROR:
//android.view.WindowManager$BadTokenException: 
//Unable to add window -- token null is not for an application
Context mContext = activity.getApplicationContext();
Dialog dialog = new Dialog(mContext);

備考:マイカスタムダイアログが作成された外activity.onCreateDialog(int dialogId)方法

試してみてください -

AlertDialog.Builder builder = new AlertDialog.Builder(getParent());

getActivity()ProgressDialog.show()を使用して、それがクラッシュした(互換性)断片と同様の問題がありました。私はそれが理由のタイミングであることに同意思います。

の可能な修正ます:

mContext = getApplicationContext();

if (mContext != null) {
    mProgressDialog = ProgressDialog.show(mContext, "", getString(R.string.loading), true);
}

の代わりに使用しての

mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);
可能な限り早期に

場所mContextはそれを文脈をつかむために多くの時間を得ました。これが動作するという保証はそれだけでクラッシュの可能性を低減し、まだありません。それはまだ動作しない場合は、(後にダイアログを却下などの他のタイミングの問題を引き起こす可能性があります)、タイマーハックに頼る必要があるだろう。

あなたがthisActivityName.thisを使用できるかどうthisはすでに何かを指しているため、もちろん、それはより安定します。しかし、いくつかのケースでは、特定のフラグメントのアーキテクチャと同じように、それはオプションではありません。

(将来の参照用)

のhttp:

私はそこのアプリケーション・コンテキストおよびアクティビティーコンテキストの違いは、のように、ここで説明したので、それはだと思います。 //www.doubleencore.com/2013/06/context/する

どの我々はアプリケーション・コンテキストを使用して、ダイアログ示すことができないことを意味します。それはそれだ。

活動内でダイアログを使用してのために、それをこのように実行します。

private Context mContext;
private AlertDialog.Builder mBuilder;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     mContext = this;

     //using mContext here refering to activity context
     mBuilder = new AlertDialog.Builder(mContext);
     //...
     //rest of the code
     //...
}

フラグメント内のダイアログを使用してのために、それをこのように実行します。

private Context mContext;
private AlertDialog.Builder mBuilder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View mRootView = inflater.inflate(R.layout.fragment_layout, container, false);
      mContext = getActivity();

      //using mContext here refering to fragment's hosting activity context
      mBuilder = new AlertDialog.Builder(mContext);
      //...
      //rest of the code
      //...
      return mRootView;
}

それはそれだ^ _ ^

私はこれは私がグローバルデータを格納するすべての私の活動のための基本クラスを作成することでした周りを取得するためにやりました。

:最初の活動では、私はそうのように私の基本クラス内の変数のコンテキストを保存しました

基本クラス

public static Context myucontext; 

は最初のアクティビティベースクラスから派生

mycontext = this
ダイアログを作成するときにそれから私は、代わりにgetApplicationContextのあるMyContextを使用しています。

AlertDialog alertDialog = new AlertDialog.Builder(mycontext).create();

あなたが活動にmContextをキャスト、フラグメントにProgressDialog.show()を呼び出している場合は、私のために働いています。

     ProgressDialog pd = new ProgressDialog((Activity) mContext);

私は現在activitty view.Whenever Iの上で投げる例外の警告ダイアログを実装しているが、このように与えられていた。

AlertDialog.Builder builder = new AlertDialog.Builder(context);

context = this;

のようにグローバルとしてsetContentView() method.Takenコンテキスト変数にonCreate()文の後のonCreate()。だから、簡単なI使用Context context;のうち、警告のために同じウィンドウException.I書き込みコードを考えます

コードサンプルです。

static Context context;

 public void onCreate(Bundle savedInstanceState)  { 
        super.onCreate(savedInstanceState); 


        setContentView(R.layout.network); 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        context = this;
.......

alertメソッドのサンプルがある。

private void alertException(String execMsg){
        Log.i(TAG,"in alertException()..."+context);
        Log.e(TAG,"Exception :"+execMsg);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
.......
me.Actually私はStackOverflowの私に、このエラーの検索それは.Iが、これは例外を克服するための簡単な解決策であると思った作品ように、この記事のすべての応答を読んで、このquery.Afterが、私はこの方法を試してみましたため、それは罰金を動作します。

おかげで、 Rajendar

あなたはgroupActivity上の問題を抱えている場合は、これを使用してはいけません。 親が親ActivityGroupから静的なものである。

final AlertDialog.Builder builder = new AlertDialog.Builder(GroupActivityParent.PARENT);

の代わりに

final AlertDialog.Builder builder = new AlertDialog.Builder(getParent());

Aダイアログ常に作成し、活動の一部として表示されます。代わりに、アプリケーションコンテキストのActivityコンテキストに渡す必要があります。

http://developer.android.com/guide/topics/ UI / dialogs.html#ShowingADialogする

これは共通の問題です。 使用thisの代わりgetApplicationContext() それは

あなたの問題を解決する必要があります
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top