解決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.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            myProvinceSpinnerLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            myProvinceSpinnerLP.addRule(RelativeLayout.RIGHT_OF,myProvinceTextView.getId());
            myProvinceSpinnerLP.setMargins(10, 0, 10, 0);
            myCityRelativeLayout.addView(myProvinceSpinner,myProvinceSpinnerLP);
       
            //定義myCityTextView對象,編號:3、內容:城市
            TextView myCityTextView =new TextView(CityManagerActivity.this);
            myCityTextView.setText(R.string.city);
            myCityTextView.setId(3);
            //定義myCityTextViewLP對象,佈局:左邊跟myProvinceTextView組件對齊、在myProvinceTextView組件下方、距左邊10px、距右邊10px
            RelativeLayout.LayoutParams myCityTextViewLP= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            myCityTextViewLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            myCityTextViewLP.addRule(RelativeLayout.BELOW,myProvinceSpinner.getId());
            myCityTextViewLP.setMargins(10, 0, 10, 0);
            myCityRelativeLayout.addView(myCityTextView,myCityTextViewLP);
           
            //定義myCityEditText對象,編號:4、內容:點擊列表的內容
             final EditText myCityEditText_add=new EditText(CityManagerActivity.this);
           
       
            //定義myCityEditTextLP對象,佈局:左邊跟myCityTextViewLP組件對齊、在myCityTextViewLP組件下方、距左邊10px、距右邊10px
            RelativeLayout.LayoutParams myCityEditTextLP= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            myCityEditTextLP.addRule(RelativeLayout.BELOW,myProvinceSpinner.getId());
            myCityEditTextLP.addRule(RelativeLayout.RIGHT_OF,myCityTextView.getId());
            myCityEditTextLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            myCityEditTextLP.setMargins(10, 0, 10, 0);
            myCityRelativeLayout.addView(myCityEditText_add,myCityEditTextLP);
           
            //定義對話框www.aiwalls.com
            new AlertDialog.Builder(CityManagerActivity.this)
            .setView(myCityRelativeLayout)
            .setIcon(R.drawable.add)
            .setTitle(R.string.add_city)
            .setPositiveButton(R.string.add, new OnClickListener() {
               
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   
                    String cityName=myCityEditText_add.getText().toString();
                    if(cityName==null||cityName.trim().length()==0)
                    {
                        Toast.makeText(CityManagerActivity.this, "城市不能為空!!!", Toast.LENGTH_LONG);
                    }else{
                        Integer provinceId=null;
                        if(provinceName!=null)
                        {
                            Cursor cursor=DBHelper.getInstance(CityManagerActivity.this).selectProvinceByName(provinceName);
                            while(cursor.moveToNext())
                            {
                                provinceId=cursor.getInt(0);
                               
                            }
                        }
                        Province province= new Province();
                        province.setId(provinceId);
                       
                        City city= new City();
                        city.setName(cityName);
                        city.setProvince(province);
                       
                        DBHelper.getInstance(CityManagerActivity.this).addCity(city);
                       
                    }
                   
                   
                   
                }
            })
            .setNegativeButton(R.string.cancel, null)
            .show();
            break;

當我再次運行這個項目時候,終於達到預期的效果瞭呢!!!我很高興!!!

就這個問題已經困擾我好幾個小時呢,剛開始我還以為Spinner那部分代碼有問題呢,我啊,各種改啊,就是沒有解決啊。我竟然都開始懷疑自己的能力呢!!!

從這個問題我體會到:

1:不要太依賴android的佈局文件,有時候你可以考慮自己手動編寫佈局代碼;

2:如果有些地方不瞭解,可以考慮看android Samples實例代碼或者看源碼;

3:當發現問題,看控制臺的異常,然後將關鍵部分去goolge網上去搜索;

4:Never give up 方法總比問題多,要靜下心去慢慢解決,既然選擇這行,就要去堅持下去。

 

摘自  stark-summer 

發佈留言