我有一个警报对话框,其中有一个微调器:
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inf.inflate(R.layout.change_bike_state, null);
AlertDialog.Builder changeState = new AlertDialog.Builder(activity);
changeState.setTitle(bikeName + " " + stateName);
changeState.setView(layout);
String state_data[] = new String[3];
state_data[0]="Alquilada";
state_data[1]="No alquilada";
state_data[2]="Mantenimiento";
Spinner states = (Spinner) layout.findViewById(R.id.bike_states);
ArrayAdapter adapter = new ArrayAdapter(activity,android.R.layout.simple_spinner_item, state_data);
states.setAdapter(adapter);
//positive and negative button
changeState.show();
change_bike_state.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/bike_states"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
显示了微调器,
但点击时崩溃。
01-28 12:42:43.114: E/AndroidRuntime(417): FATAL EXCEPTION: main
01-28 12:42:43.114: E/AndroidRuntime(417): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-28 12:42:43.114: E/AndroidRuntime(417): at android.view.ViewRoot.setView(ViewRoot.java:509)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.app.Dialog.show(Dialog.java:241)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.widget.Spinner.performClick(Spinner.java:257)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.view.View$PerformClick.run(View.java:8816)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.os.Handler.handleCallback(Handler.java:587)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.os.Looper.loop(Looper.java:123)
01-28 12:42:43.114: E/AndroidRuntime(417): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-28 12:42:43.114: E/AndroidRuntime(417): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 12:42:43.114: E/AndroidRuntime(417): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 12:42:43.114: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-28 12:42:43.114: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-28 12:42:43.114: E/AndroidRuntime(417): at dalvik.system.NativeStart.main(Native Method)
如何解决这个问题?