Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window

前端 未结 6 1406
滥情空心
滥情空心 2020-11-29 10:43

I want to set the spinner value using String[] or ArrayList.

I have done spinner in other activity working fine.In this activity inside the Tab acivityGroup another

相关标签:
6条回答
  • 2020-11-29 11:06
    viewToLoad = getLayoutInflater().inflate(R.layout.line_discount, null);
    (viewToLoad.getContext(), android.R.layout.simple_spinner_item, proList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    

    This did the job for me

    0 讨论(0)
  • 2020-11-29 11:08

    I tried with code.Its working fine:

     View viewToLoad;
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewToLoad = LayoutInflater.from(getDialogContext(this)).inflate(R.layout.header_discount, null);
        this.setContentView(viewToLoad); 
    
         ArrayAdapter<String> adapter = new ArrayAdapter<String> (viewToLoad.getContext(), android.R.layout.simple_spinner_item, proList);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
         headerDisProdCode.setAdapter(adapter);
    
         headerDisProdCode.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,int arg2, long arg3) {
                seletcedProductName = parent.getSelectedItem().toString();
                seletcedProductCode = (products.get((int) headerDisProdCode.getSelectedItemId())).getProductCode();
    
            }
    
            public void onNothingSelected(AdapterView<?> arg0) {
    
            }
        });
    

    }

    ArrayAdapter context I gave like : viewToLoad.getContext() viewToLoad is the inflate

    0 讨论(0)
  • 2020-11-29 11:17

    I had this problem as well, but with Fragments, when adding a fragment to a view, and the answer for me was to pass in getApplicationContext(), but I had to do it through a separate method after instantiating the fragment, since it was requiring the use of a Bundle.

    I also had to do the following when inflating the view, using the context passed in above:

    View v = inflater.from(context).inflate(R.layout.layout_name, ViewGroup container, T/F);
    

    rather than just:

    View v = inflater.from(context).inflate(R.layout.layout_name, ViewGroup container, T/F);
    

    Hope this helps somebody struggling with Fragments.

    0 讨论(0)
  • 2020-11-29 11:25

    I think you have context problem.Try to get context using below method

    you can create a new activity and set its theme to dialog theme so that when you start your activity it will display as dialog. For more information about dialog see below post

    Click here

    EDIT2

    I found a solution for badTokenExcaption

    In your activity's onCreate() method replace the line setContentView(R.layout.XXXXX) by

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.XXXXX, null);
    this.setContentView(viewToLoad); 
    

    and replace the spinner code by following lines

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.medicine_types, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spDosageType.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-11-29 11:25

    It's obvious from the error message that the problem is with context used for creating the spinner. Try this

    viewToLoad = LayoutInflater.from(this).inflate(R.layout.line_discount, null);
    

    Or:

    viewToLoad = getLayoutInflater().inflate(R.layout.line_discount, null);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                            android.R.layout.simple_spinner_item, proList);
    sp.setAdapter(adapter);
    
    0 讨论(0)
  • 2020-11-29 11:26

    When you create your ArrayAdapter you should pass the Context of your ActivityGroup not the Context of your current Activity.

    Here is an example of how I get it:

      public class MyActivityGroup extends ActivityGroup{
           pulbic static MyActivityGroup sGroup;
    
           protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                sGroup=this;
                //...
           }
      }
    
      // Tab Implementation
      //.....
      ArrayAdapter<String> adapter = new ArrayAdapter<String> (
              MyActivityGroup.sGroup, android.R.layout.simple_spinner_item, proList);
    
    0 讨论(0)
提交回复
热议问题