当前位置:操作系统 > 安卓/Android >>

解决Spinner不能在Tabhost中使用

本想昨天晚上写这篇文章,但是我学校的网络真的太不给力了呢!竟然无法打开这个页面啊!!!
述说正文吧:
程序背景:使用TabHost,需要在子界面中添加Spinner按钮。
问题:Spinner按钮不能点击,点击出现unable to add window...is your activity running?的错误。
探索:
    由于点击Spinner之后,会弹出一个dropdown item的选项卡,我认为unable to add window应该是这个选项卡没有可以添加上去的activity。从错误的最后一句也能看出程序认为它需要的那个activity并没有在运行。那 Spinner究竟需要哪个activity?哪个activity是当前在运行的?
    思前想后,没有想明白。在网上找了些资料,有一个博客上是这么说 AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 。我按照这个来试了一下,不加this.getParent()会出现上面的错误,加了就没问题。由此可见当前activity应该是tabhost,而不是每个tab对应的activity。
    问题又来了,我应该在哪里给Spinner把当前的activity改成this.getParent()?我在程序里是用了 (Spinner)findViewById(...)的。一个简单的方法就是直接用new Spinner(Context context, AttributeSet attributeSet),这里的context用this.getParent()应该行。不过我这人比较懒,不喜欢用AttributeSet,界面的全放到layout里面。
    又纠结了一个晚上,看来网上有人说不能用setContentView(R.layout...)。一下子就醒悟过来,肯定是这个家伙在搞鬼,是它生成Spinner的。于是把代码改成下面这样子:
  View contentView = LayoutInflater.from(this.getParent()).inflate(R.layout.search_activity, null);
  //setContentView(R.layout.search_activity);
  setContentView(contentView);
    现在问题终于解决了...我只能说不去看一下android的底层实现代码就不能避免这样的囧况(这问题可是困扰了我很久,之前实在搞不定就绕了过去)。

以上红色代码是我在网上找的资料;
按照上面提及的方法我也试了一遍,如果你的程序中Spinner不是放在Dialog中,那个你可以是使用
View contentView = LayoutInflater.from(this.getParent()).inflate(R.layout.search_activity, null);
  //setContentView(R.layout.search_activity);
  setContentView(contentView);
方法来解决如图1,没有任何问题的。

但是我现在想在Dialog中使用Spinner,按照上面的做法不好用。也许是我了解的不够深入吧。

那个时候我想能不能是android layout XML布局文件的问题,如果我用java代码写一个Dialog对话框(包括:Spinner和EditText组件)。

代码如下:

    RelativeLayout myCityRelativeLayout=null;
        TextView myProvinceTextView=null;
        Spinner myProvinceSpinner=null;
        ArrayAdapter<String> myProvinceArrayAdapter=null;
        ArrayList<String> provinces=null;
        RelativeLayout.LayoutParams myProvinceTextViewLP=null;
        Cursor cursor=null;
        final Spinner spinner=null;
       
        switch (item.getItemId()) {
        case MENU_ADD:
           
             myCityRelativeLayout= new RelativeLayout(CityManagerActivity.this);
            //设置相对布局宽度和高度
            myCityRelativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
            //设置相对布局中组件为向左对齐
            myCityRelativeLayout.setGravity(Gravity.LEFT);
           
            //定义TextView对象myProvinceTextView、编号为1、内容为省份
            myProvinceTextView=new TextView(CityManagerActivity.this);
            myProvinceTextView.setId(1);
            myProvinceTextView.setText(R.string.province);
            //定义myProvinceTextViewLP对象,布局:为顶部布局、跟父类左侧对齐,距左边10px
             myProvinceTextViewLP= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            myProvinceTextViewLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            myProvinceTextViewLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            myProvinceTextViewLP.setMargins(10, 0, 10, 0);
            myCityRelativeLayout.addView(myProvinceTextView,myProvinceTextViewLP);
       
            //定义myProvinceSpinner对象、编号为2
             myProvinceSpinner = new Spinner(CityManagerActivity.this);
           
            cursor=DBHelper.getInstance(CityManagerActivity.this).selectProvince();
            myProvinceArrayAdapter= new ArrayAdapter<String>(CityManagerActivity.this, android.R.layout.simple_spinner_item);
            myProvinceArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           
            while(cursor.moveToNext())
            {
                System.out.println(cursor.getString(1));
                myProvinceArrayAdapter.add(cursor.getString(1));
            }
           
           
            myProvinceSpinner.setAdapter(myProvinceArrayAdapter);
            myProvinceSpinner.setId(2);
            provinceName=myProvinceSpinner.getSelectedItem().toString();
            //System.out.println(provinceName);
            myProvinceSpinner.setOnItemSelectedListener(myProvinceSpinnerListener);
            //定义myProvinceSpinnerLP对象,布局:为顶部布局、跟父类左侧对齐、距左边10px、距右边10px
            RelativeLayout.LayoutParams myProvinceSpinnerLP= new RelativeLayout.Layo

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,