6

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 Tab activity.

My problem is setting values into spinner. Spinner is displaying correctly Thay means when load the activity, that is working fine but when I click On spinner its give error:

Error is :

    09-30 16:11:37.693: ERROR/AndroidRuntime(699): FATAL EXCEPTION: main
09-30 16:11:37.693: ERROR/AndroidRuntime(699): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@407f4de8 is not valid; is your activity running?
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.ViewRoot.setView(ViewRoot.java:527)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.Dialog.show(Dialog.java:241)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.widget.Spinner.performClick(Spinner.java:260)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.view.View$PerformClick.run(View.java:9080)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Handler.handleCallback(Handler.java:587)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.os.Looper.loop(Looper.java:123)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at java.lang.reflect.Method.invoke(Method.java:507)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 16:11:37.693: ERROR/AndroidRuntime(699):     at dalvik.system.NativeStart.main(Native Method)

This is my code :

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

   ArrayList<String> productList = new ArrayList<String>();
    int size = products.size()+1;
    String[] proList = new String[size];
    proList[0] = "---Select----";

    for(int i = 1; i< size ;i++){
        productList.add(products.get(i-1).getDescription());
        proList[i] = products.get(i-1).getDescription();
    }

    sp = (Spinner)findViewById(R.id.spProList);
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (LineDiscountActivity.this, android.R.layout.simple_spinner_item, proList);
    sp.setAdapter(adapter);

This is my image:

enter image description here

Problem in TabActivity.Because I have run this part Within the TabActivityGroup. Its was working.When I run this inside the Tab Activity within TabActivityGroup, then its a problem. I have TabActivtyGroup &Within that normal TabActivity.

How can I do in this situation?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Piraba
  • 6,974
  • 17
  • 85
  • 135
  • 1
    Please refer to my answer for this [question][1]. [1]: http://stackoverflow.com/questions/4568494/error-while-placing-a-spinner-inside-activity-group – Anju Sep 30 '11 at 11:16
  • 2
    You are using a bad context somewhere, try to use the exact context - Suri Sahani.... – Lalit Poptani Sep 30 '11 at 11:17
  • 1
    No.Its not working.Whereever context need It gave it ,ClassName.this And I tried this also 'View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.line_discount, null); this.setContentView(viewToLoad); ' – Piraba Sep 30 '11 at 11:41
  • Did you try Mathews answer in http://stackoverflow.com/questions/4568494/error-while-placing-a-spinner-inside-activity-group ? It worked for me. – Sujith Oct 04 '11 at 06:18
  • Yes I tried but not working .Is you activity is within the normal Activity that is within the Tab ActivityGroup? – Piraba Oct 04 '11 at 06:22

6 Answers6

28

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);
Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129
  • I had give you another way to solve your issue.It may help you – Dharmendra Oct 05 '11 at 12:27
  • 1
    The EDIT2 code solves this situation, thanks a lot Dharmendra! – jcamacho May 25 '12 at 09:37
  • 1
    +1 for EDIT2 , after coming back to Android after 18 months of iOS it driving me mad - thanks for the great advice. – Chris Sep 06 '12 at 12:12
  • Hi can you take a look at this? This is a similar scenario but i can't get the correct context no matter what. i've been stuck with this for two days now. Hope you can help http://stackoverflow.com/questions/36837504/unable-to-add-window-token-is-not-valid-error-when-clicked-on-a-spinner – k9yosh Apr 26 '16 at 13:44
3

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);
halfer
  • 19,824
  • 17
  • 99
  • 186
Ron
  • 24,175
  • 8
  • 56
  • 97
  • NOt working.This activity come under normal tab activity within the tab Activity Group – Piraba Oct 03 '11 at 09:57
  • try using just `this` as context for creating adapter. – Ron Oct 04 '11 at 06:48
  • Does it give the same exception? – Ron Oct 05 '11 at 13:37
  • Yes.I couldn't get the result.Result is loading in the spinner.But when I click spinner only problem.I really don't know how to get the context.I tried several way.I couldn't get the result... – Piraba Oct 06 '11 at 03:17
  • Thanks for help...I did like this working fine.. `ArrayAdapter adapter = new ArrayAdapter (viewToLoad.getContext(), android.R.layout.simple_spinner_item, proList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);` – Piraba Oct 22 '11 at 04:07
  • you missed `this.setContentView(viewToLoad);` – Houcine Feb 09 '13 at 12:30
2

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);
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
2

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

Piraba
  • 6,974
  • 17
  • 85
  • 135
0

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.

craned
  • 2,991
  • 2
  • 34
  • 38
0
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

cmario
  • 605
  • 1
  • 7
  • 22