我有一个警告对话框,其中有一个微调器:
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)
如何解决这个问题?
答案 0 :(得分:1)
在Alertdialog
而不是activity
中传递活动上下文。您的对话框无法找到它应显示在哪个活动上下文中。请尝试以下方法:
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inf.inflate(R.layout.view_map, null); AlertDialog.Builder changeState = new AlertDialog.Builder( context); 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(context, android.R.layout.simple_spinner_item, state_data); states.setAdapter(adapter); // positive and negative button changeState.show();
答案 1 :(得分:0)
View layout = inf.inflate(R.layout.change_bike_state, (ViewGroup)getCurrentFocus());
答案 2 :(得分:0)
试试这个,
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(YourActivity.this);
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
AlertDialog alert = changeState.create();
alert.show();
答案 3 :(得分:0)
使用这种方式来夸大AlertDialog's Layout
View layout = inf.inflate(R.layout.change_bike_state, null, true);
而不是
View layout = inf.inflate(R.layout.change_bike_state, null);