简体   繁体   中英

Android spinner problem

when i click on spinner my application crashes ans it throws an exception like "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application "

 String[] s = new String[60];
        int i;
        for (i = 0; i < 60; i++) {
            s[i] = Integer.toString(i + 1);
        }

        Spinner spin = (Spinner) findViewById(R.id.TimeSpinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, s);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(new MyOnItemSelectedListener());

class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {
            spvalue = parent.getItemAtPosition(pos).toString();

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    }

enter code here Logcat

I/ActivityManager(   66): Displayed activity com.imt.main/.TimeSpinner: 503 ms (total 503 ms)
W/WindowManager(   66): Attempted to add window with non-application token WindowToken{43fc5cf8 token=null}.  Aborting.
D/AndroidRuntime( 1382): Shutting down VM
W/dalvikvm( 1382): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime( 1382): FATAL EXCEPTION: main
E/AndroidRuntime( 1382): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 1382):        at android.view.ViewRoot.setView(ViewRoot.java:509)
E/AndroidRuntime( 1382):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
E/AndroidRuntime( 1382):        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/AndroidRuntime( 1382):        at android.app.Dialog.show(Dialog.java:241)
E/AndroidRuntime( 1382):        at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
E/AndroidRuntime( 1382):        at android.widget.Spinner.performClick(Spinner.java:257)
E/AndroidRuntime( 1382):        at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime( 1382):        at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1382):        at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1382):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1382):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 1382):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1382):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 1382):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 1382):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 1382):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(   66):   Force finishing activity com.imt.main/.TimeSpinner
W/ActivityManager(   66): Activity pause timeout for HistoryRecord{43f92d70 com.imt.main/.TimeSpinner}

I am facing same issue and now have solved it. I am using GroupActivity for Tab based application.

If you are using GroupActivity and if this problem occurs then it is just because of the context. You are not able to pass the right context to the spinner and it gives error

Now the Solution is:

  1. Create any layout like LinearLayout in place of the spinner in your xml
  2. Create Spinner dynamically at java code like below and add that to your linearLayout:

    Spinner yourSpinner = new Spinner(getParent()); // dynamically Spinner yourLinearLayout.addView(yourDriverSpinner); // your linearlayout

  3. Now, do whatever you want to do with spinner.

This help me a lot and will also help you.

Still if there is any error then let me know.

Enjoy.

:)

Define this at class level as member variable

String[] s = new String[60];

replace this line

spvalue = parent.getItemAtPosition(pos).toString();

with this

spvalue = s[pos];

here is simple spinner example..

spinner.java

package com.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class spinner extends Activity {
/** Called when the activity is first created. */
private String array_spinner[];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

array_spinner = new String[5];
array_spinner[0] = "Name";
array_spinner[1] = "Address";
array_spinner[2] = "Phone";
array_spinner[3] = "Mobile";
array_spinner[4] = "Home";
Spinner s = (Spinner) findViewById(R.id.spin);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);

}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spin"
/>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM