6

我的应用程序如果 poulsas 一个按钮是一个有两个微调器的弹出窗口,那么我可以在那里弹出窗口,但是当我得到错误时,是我点击微调器时。

在这里,您有我的以下代码和调试,因为 logcat 我一切正常。

 public void añadirRegistro(View v){

         showPopup(leer_registros.this);
    }
    private void showPopup(final Activity context) {

           Spinner eleccionIP,eleccionRegistro;
           borrar_datos BorrarDatos = new borrar_datos ();
           // Inflate the popup_layout.xml
           RelativeLayout viewGroup = (RelativeLayout) context.findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View layout = layoutInflater.inflate(R.layout.popup_elegir_registros, viewGroup);


           eleccionIP = (Spinner) layout.findViewById(R.id.popupIP);
           eleccionRegistro = (Spinner)layout.findViewById(R.id.popupRegistro);



           /*Cursor cur=BorrarDatos.obtenerIP();
           BorrarDatos.rellenarSpinner(cur,eleccionIP);*/


           final PopupWindow popup = new PopupWindow(context);
           popup.setContentView(layout);
           popup.setWidth(LayoutParams.WRAP_CONTENT);
           popup.setHeight(LayoutParams.WRAP_CONTENT);
           popup.setFocusable(true);

           popup.showAtLocation(layout, Gravity.NO_GRAVITY, 200, 200);


           ArrayAdapter <CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item);
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

           adapter.add("item 1");
           adapter.add("item 2");
           eleccionIP.setAdapter(adapter);




    }

调试:

WindowManagerGlobal.addView(View, ViewGroup$LayoutParams, Display, Window) line: 255    
WindowManagerImpl.addView(View, ViewGroup$LayoutParams) line: 69    
PopupWindow.invokePopup(WindowManager$LayoutParams) line: 993   
PopupWindow.showAsDropDown(View, int, int) line: 899    
Spinner$DropdownPopup(ListPopupWindow).show() line: 603 
Spinner$DropdownPopup.show() line: 981  
Spinner.performClick() line: 609    
View$PerformClick.run() line: 17355 
Handler.handleCallback(Message) line: 725   
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
Looper.loop() line: 137 
ActivityThread.main(String[]) line: 5041    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 511  
ZygoteInit$MethodAndArgsCaller.run() line: 793  
ZygoteInit.main(String[]) line: 560 
NativeStart.main(String[]) line: not available [native method]  

我的第二个错误的调试:

Scada [Android Application] 
    DalvikVM[localhost:8636]    
        Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))   
            WindowManagerGlobal.addView(View, ViewGroup$LayoutParams, Display, Window) line: 255    
            WindowManagerImpl.addView(View, ViewGroup$LayoutParams) line: 69    
            PopupWindow.invokePopup(WindowManager$LayoutParams) line: 993   
            PopupWindow.showAsDropDown(View, int, int) line: 899    
            Spinner$DropdownPopup(ListPopupWindow).show() line: 603 
            Spinner$DropdownPopup.show() line: 981  
            Spinner.performClick() line: 609    
            View$PerformClick.run() line: 17355 
            Handler.handleCallback(Message) line: 725   
            ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
            Looper.loop() line: 137 
            ActivityThread.main(String[]) line: 5041    
            Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
            Method.invoke(Object, Object...) line: 511  
            ZygoteInit$MethodAndArgsCaller.run() line: 793  
            ZygoteInit.main(String[]) line: 560 
        Thread [<10> Binder_2] (Running)    
        Thread [<9> Binder_1] (Running) 
        Thread [<11> AsyncTask #1] (Running)    
        Thread [<12> AsyncTask #2] (Running)    
        Thread [<13> AsyncTask #3] (Running)    
        Thread [<14> AsyncTask #4] (Running)    
        Thread [<15> AsyncTask #5] (Running)

我做了调试,错误一定是在这里:

RelativeLayout viewGroup = (RelativeLayout) ((Activity) context).findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = layoutInflater.inflate(R.layout.popup_elegir_registros, viewGroup);


           eleccionIP = (Spinner)layout.findViewById(R.id.popupIP);
           eleccionRegistro = (Spinner)layout.findViewById(R.id.popupRegistro);
4

5 回答 5

19

在微调器中添加android:spinnerMode="dialog"解决了我的问题。

如何在 Android 的 PopupWindow 内创建 Spinner 小部件?单击 Spinner 时获取 BadTokenException

于 2014-03-10T20:39:51.307 回答
4

我解决了同样的问题。在 xml 类中添加你的微调器。

android:spinnerMode="dialog"
于 2016-04-04T10:01:12.647 回答
0

用这个替换适当的行,看看会发生什么:

final View layout = layoutInflater.inflate(R.layout.popup_elegir_registros, new ViewGroup(this) {

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

    }
});

    eleccionIP = (Spinner) layout.findViewById(R.id.popupIP);
    eleccionRegistro = (Spinner)layout.findViewById(R.id.popupRegistro);



    /*Cursor cur=BorrarDatos.obtenerIP();
    BorrarDatos.rellenarSpinner(cur,eleccionIP);*/



    final PopupWindow popup = new PopupWindow(context);
    popup.setContentView(layout);
    popup.setWidth(LayoutParams.WRAP_CONTENT);
    popup.setHeight(LayoutParams.WRAP_CONTENT);
    popup.setFocusable(true);

    new Handler().postDelayed(new Runnable(){

        public void run() {
            popup.showAtLocation(layout, Gravity.NO_GRAVITY, 200, 200);
        }

    }, 100L);

    ArrayAdapter <CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    adapter.add("item 1");
    adapter.add("item 2");
    eleccionIP.setAdapter(adapter);

将有助于创建一个弹出窗口,

于 2013-10-21T11:01:03.650 回答
0

如果要在弹出窗口中设置微调器,请执行以下操作:

//this your inside layout
final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//and that is your spinner
ArrayAdapter<String> adapterState =
                new ArrayAdapter<String>(
                        layoutInflater.getContext(),
                        android.R.layout.simple_spinner_item,
                        keysState);

        adapterState.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        spinnerState.setAdapter(adapterState);

使用 layout.getContext() 而不是 getApplicationContext() 或 Activty.this 等,因为您在另一个布局视图中。

于 2015-12-24T04:13:21.583 回答
-1

尝试这个,

//声明你的数组列表拳头并添加一些itms

  private ArrayList<String> item = new ArrayList<String>();
  item.add("1");
  item.add("2");

// 像这样改变你的数组适配器

  ArrayAdapter <CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  eleccionIP.setAdapter(adapter);
于 2013-10-21T06:45:35.570 回答