1

私はこの問題の解決策をウェブで探していましたが、残念ながら答えが見つからないようです。スピナーを内部に持つPopupWindowのXMLファイルを作成しました。ボタンイベントリスナー内で、次のコードを呼び出してPopupWindowを膨らませ、画面に表示します。

LayoutInflater inflater = getLayoutInflater();
settings_layout = inflater.inflate(R.layout.setting_popout, (ViewGroup) findViewById(R.id.setting_popout));

// Creates a popup window of required width and height, and displays
// the popup in the center of the screen.
pw_settings = new PopupWindow(settings_layout, 400, 470, true); 
pw_settings.showAtLocation(settings_layout, Gravity.CENTER, 0, 0);

spColors = (Spinner) settings_layout.findViewById(R.id.linecolor);

// Sets the initial values of the color spinner and the listener
ArrayAdapter<CharSequence> adapter_color = 
    ArrayAdapter.createFromResource(this, R.array.colors_array, android.R.layout.simple_spinner_item);
adapter_color.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spColors.setAdapter(adapter_color);
spColors.setSelection(adapter_color.getPosition(over.color));

ボタンをクリックすると、ポップアップウィンドウが正常に表示されます。ただし、スピナーをクリックすると、LogCatで次のエラーが発生します。

android.view.WindowManager $ BadTokenException:ウィンドウを追加できません-トークンandroid.view.ViewRootImpl $ W@41402a90は無効です。あなたの活動は実行されていますか?..。

何が間違っているのかわかりません。どんな助けでも大歓迎です!ありがとうございました!

4

3 に答える 3

11

少し遅れて、元の質問に対する正確な答えではないかもしれませんが、ここでの別の質問から、スピナーのxmlに次の行を挿入すると、エラーの発生が防止されたことがわかりました。

android:spinnerMode="dialog"

于 2014-01-14T15:06:29.570 に答える
1

私はこの問題に数回遭遇しました、そして私が見つけた唯一の時間のかからない方法は上記のマイク・フォスカーによって提案されたものです。

android:spinnerMode="dialog"

スピナーのオプションリストが別のポップアップに表示されるようにします。これにより、コードで開いた最初のポップアップウィンドウが作動しなくなります。例えば ​​:

<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
于 2014-09-17T13:09:57.827 に答える
0

試す:

settings_layout = inflater.inflate(R.layout.setting_popout, null);
((ViewGroup) findViewById(R.id.setting_popout)).addView(settings_layout);

ViewGroupまた、ポップアップのレイアウトをすでにアクティビティ内にあるレイアウトに添付するのはなぜですか?たぶん、あなたはただ逃げることができます:

settings_layout = inflater.inflate(R.layout.setting_popout, null);
于 2012-07-10T19:44:35.333 に答える