Spinner动态错误

时间:2013-06-04 16:37:59

标签: android spinner

我在Android应用中遇到错误。我有一个类LineaArticulo动态来形成一个带有标签的行,编辑文本,一个按钮和微调器,收紧微调器我得到一个错误,附在这里的类构造函数。

所有数据都是正确的。

这里只是放置微调框。

public LineaArticulo(RelativeLayout layoutactual, final String[] Casos, Context context, int Xspinner, int Yspinner){

    final Spinner spiner = new Spinner(context);

    ArrayAdapter<String> adaptador =
            new ArrayAdapter<String>(context,
                    android.R.layout.simple_spinner_item, Casos);

    adaptador.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);

    spiner.setAdapter(adaptador);

    RelativeLayout.LayoutParams paramSpiner = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    paramSpiner.leftMargin = Xspinner; // Your X coordinate
    paramSpiner.topMargin = Yspinner; // Your Y coordinate
    spiner.setLayoutParams(paramSpiner);

    layoutactual.addView(spiner);
}

错误:

  06-04 16:02:02.021      961-961/compra.online.mercavalencia    E/AndroidRuntime: FATAL EXCEPTION: main
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:531)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)
    at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
    at android.widget.Spinner.performClick(Spinner.java:260)
    at android.view.View$PerformClick.run(View.java:9080)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

通常你应该使用应用程序Context进行祝酒,使用Activity进行对话。

正如您所说,问题在于AlertDialogSpinner内部使用。

  

AlertDialog $ Builder.show(AlertDialog.java:802)

Similar entry