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(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)

如何解决这个问题?

4

4 回答 4

1

Alertdialog在你的not中传递活动上下文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();
于 2013-01-28T13:08:00.707 回答
0
View layout = inf.inflate(R.layout.change_bike_state, (ViewGroup)getCurrentFocus());
于 2013-01-28T13:05:06.860 回答
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();
于 2013-01-28T13:20:05.097 回答
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);
于 2013-01-28T13:27:40.637 回答