Windows Manager,单击自定义Spinner时出现错误令牌异常

时间:2012-12-10 07:15:44

标签: android spinner

点击Spinner时出现此错误。

12-06 16:34:52.884: E/AndroidRuntime(21663): FATAL EXCEPTION: main
12-06 16:34:52.884: E/AndroidRuntime(21663): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@406a13c0 is not valid; is your activity running?
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.view.ViewRoot.setView(ViewRoot.java:445)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.view.Window$LocalWindowManager.addView(Window.java:526)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.app.Dialog.show(Dialog.java:269)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.app.AlertDialog$Builder.show(AlertDialog.java:907)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.widget.Spinner$DialogPopup.show(Spinner.java:664)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.widget.Spinner.performClick(Spinner.java:427)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.view.View$PerformClick.run(View.java:11928)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.os.Handler.handleCallback(Handler.java:587)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.os.Looper.loop(Looper.java:132)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at android.app.ActivityThread.main(ActivityThread.java:4025)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at java.lang.reflect.Method.invoke(Method.java:491)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-06 16:34:52.884: E/AndroidRuntime(21663):    at dalvik.system.NativeStart.main(Native Method)
12-06 16:34:53.034: D/dalvikvm(21663): GC_CONCURRENT freed 103K, 4% free 7454K/7687K, paused 6ms+17ms
12-06 16:34:55.214: I/Process(21663): Sending signal. PID: 21663 SIG: 9

我已经通过以下链接,并没有找到我的问题的解决方案。所以请不要给我任何链接:

1。link one

2。link two

这是我的代码。

public class BeatSelectionActivity extends Activity
{

    private Button buttonBSBack,buttonBSEdit;
    private ListView listViewBSlist;
    private String[] beatName,beatNameid;
    private String[] outletName,outleId;
    private Typeface typeface;
    private TextView textViewBSTitle,textViewBSRoute;
    private Spinner spinnerBSBeatID;
    BeatSelectionHelper beatSelectionHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lyt_beat_selection);

        buttonIdSeter();

        Object[] obj=beatSelectionHelper.getBeatInfo();
        beatNameid = (String[]) obj[0];
        beatName = (String[]) obj[1];
        spinnerBSBeatID.setAdapter(new RouteSelBaseAdapter());
        spinnerBSBeatID.setOnItemSelectedListener(new RouteItemSelectedListener());
    }
    @Override
    public void onBackPressed() {}
    void buttonIdSeter()
    {
        textViewBSTitle = (TextView) findViewById(R.id.textViewBSTitle);
        textViewBSRoute = (TextView) findViewById(R.id.textViewBSroute);
        typeface = Typefaces.get(getApplicationContext(), "Vietnamese.ttf");

        beatSelectionHelper = new BeatSelectionHelper(BeatSelectionActivity.this);
        ButtonOnClickListener buttonOnClickListener= new ButtonOnClickListener();

        buttonBSBack = (Button) findViewById(R.id.buttonBSBack);
        buttonBSEdit = (Button) findViewById(R.id.buttonBSEdit);

        spinnerBSBeatID = (Spinner) findViewById(R.id.spinnerBSBeatID);

        textViewBSTitle.setTypeface(typeface);
        textViewBSRoute.setTypeface(typeface);
        buttonBSBack.setTypeface(typeface);
        buttonBSEdit.setTypeface(typeface);

        listViewBSlist = (ListView) findViewById(R.id.listViewBSlist);

        buttonBSBack.setOnClickListener(buttonOnClickListener);
        buttonBSEdit.setOnClickListener(buttonOnClickListener);
    }


    class BeatSelBaseAdapter extends BaseAdapter
    {

        View row ;
        TextView textViewBSLOutlet;
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return outletName.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return outletName[arg0];
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {

            row = convertView;

            if (row == null)
            {
                //LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LayoutInflater vi = LayoutInflater.from(getParent());
                row = vi.inflate(R.layout.lyt_beat_selection_listview,null);
            }
            textViewBSLOutlet = (TextView)row.findViewById(R.id.textViewBSLOutlet);
            textViewBSLOutlet.setTypeface(typeface);
            textViewBSLOutlet.setText(outletName[position]);
            return row;
        }
    }

    class ListViewLongClickListener implements OnItemLongClickListener
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
        {
            //pass two string(RetailerName,RetailerID) to OrderScreenActivity  



            Intent goOrderScreenActivity = new Intent(getParent(), CustomerInfoActivity.class);
            goOrderScreenActivity.putExtra("Flag", 1);
            goOrderScreenActivity.putExtra("outletName", outletName[arg2]);
            goOrderScreenActivity.putExtra("outletId", outleId[arg2]);
            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            parentActivity.startChildActivity("CustomerInfoActivity", goOrderScreenActivity);
            //startActivity(goOrderScreenActivity);

            /*Intent goOrderScreenActivity = new Intent(getParent(), EditActivity.class);
            TabGroupActivity parentActivity = (TabGroupActivity)getParent();
            parentActivity.startChildActivity("EditActivity", edit);
            return true;*/

            return false;
        }
    }



    class ButtonOnClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
            case R.id.buttonBSBack:
                //finish();
                break;

            case R.id.buttonBSEdit:
                Intent i = new Intent(getParent(), OrderModifyActivity.class);
                TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                parentActivity.startChildActivity("OrderModifyActivity", i);
                //startActivity(i);
                break;
            }
        }
    }


    class RouteSelBaseAdapter extends BaseAdapter
    {

        View row ;
        TextView textViewBSSBeatID;
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return beatName.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return beatName[arg0];
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            row = convertView;
            if (row == null)
            {
                //LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LayoutInflater vi = LayoutInflater.from(getParent());
                row = vi.inflate(R.layout.lyt_beat_selection_spinner,null);
            }
            textViewBSSBeatID = (TextView)row.findViewById(R.id.textViewBSSBeatID);
            textViewBSSBeatID.setTypeface(typeface);
            textViewBSSBeatID.setText(beatName[position]);
            return row;
        }
    }

    class RouteItemSelectedListener implements OnItemSelectedListener
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Object [] obj =beatSelectionHelper.getOutletInfo(beatNameid[arg2]);
            outletName=(String[]) obj[1];
            outleId=(String[]) obj[0];
            listViewBSlist.setAdapter(new BeatSelBaseAdapter());
            listViewBSlist.setOnItemLongClickListener(new ListViewLongClickListener());

        }

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

        }

    }

}

1 个答案:

答案 0 :(得分:2)

我走过这个问题,浪费了大约2天试图找到解决方案。但是,我没有从网上找到解决方案。

解决方案是从布局中指定Spinner模式,如下所示:

android:spinnerMode="dialog"  

或者这个:

<Spinner style="@android:style/Widget.Spinner.DropDown" ... />

或来自java代码:

Spinner(Context context, int mode)

我希望我的回答很有帮助