java.lang.IllegalArgumentException:视图未附加到窗口管理器

时间:2011-06-07 11:21:41

标签: android exception orientation

我正在使用以下代码。当我将方向从纵向更改为横向,然后再次更改为纵向。它给出了Exception“视图没有附加到窗口管理器”。 如何解决这个问题呢。 谢谢。

public class Search_List_Activity extends Activity{
public static String keyword=null, category_get_id;
public static ArrayList<String> type= new ArrayList<String>();
public static ArrayList<String> title= new ArrayList<String>();
public static ArrayList<String> des= new ArrayList<String>();
public static ArrayList<String> manu= new ArrayList<String>();
public static ArrayList<String> urls= new ArrayList<String>();
public static ArrayList<String> image_sm= new ArrayList<String>();
public static ArrayList<String> image_me= new ArrayList<String>();
public static ArrayList<String> image_la= new ArrayList<String>();
public static ArrayList<String> image_ex= new ArrayList<String>();
public static ArrayList<String> rel= new ArrayList<String>();
public static ArrayList<String> id= new ArrayList<String>();
public static ArrayList<String> cat_id= new ArrayList<String>();
public static ArrayList<String> min_price= new ArrayList<String>();
public static ArrayList<String> max_price= new ArrayList<String>();
public static ArrayList<String> stores= new ArrayList<String>();

public static ArrayList<String> offer_sku = new ArrayList<String>();
public static ArrayList<String> offer_detailurl = new ArrayList<String>();
public static ArrayList<String> offer_price = new ArrayList<String>();
public static ArrayList<String> offer_ori_price = new ArrayList<String>();
public static ArrayList<String> offer_markdownperct = new ArrayList<String>();
public static ArrayList<String> offer_shipcost = new ArrayList<String>();
public static ArrayList<String> offer_tax = new ArrayList<String>();
public static ArrayList<String> offer_total_price = new ArrayList<String>();
public static ArrayList<String> offer_bidded = new ArrayList<String>();
public static ArrayList<String> offer_merchant_prod_id = new ArrayList<String>();
public static ArrayList<String> offer_merchant_name = new ArrayList<String>();
public static ArrayList<String> offer_merchant_rating_img = new ArrayList<String>();
public static ArrayList<String> offer_merchant_logo = new ArrayList<String>();
public static ArrayList<String> offer_condition = new ArrayList<String>();
public static ArrayList<String> offer_stock = new ArrayList<String>();
public static ArrayList<String> offer_ship_amt = new ArrayList<String>();
public static ArrayList<String> offer_ship_type = new ArrayList<String>();
public static ArrayList<String> offer_id = new ArrayList<String>();
public static ArrayList<String> offer_cat_id = new ArrayList<String>();
public static ArrayList<String> offer_merchant_id = new ArrayList<String>();
public static ArrayList<String> category_id = new ArrayList<String>();
public static ArrayList<String> category_name = new ArrayList<String>();
public static ArrayList<String> offer_rating = new ArrayList<String>();
public static ArrayList<HashMap<String, Object>> mylist;
public static URL url1, url2;
public static JSONObject price;
public static String prod_type;
public static Boolean type_po;
public static Bitmap bmp;
 public static String prod_id;
 public static Boolean cat_flag;
static final int CATEGORIES_PRODUCE_ID = 0;
Dialog dialog_category;
 Bundle bundle;
 SQLiteDatabase db;
// TextView txt_search;
 private String lv_arr[]={"Cell Phone Cases","NCAA Shop","MLB Shop","NBA Shop","NFL Shop","MP3 Player Cases and Skins","iPhone Cases and Skins"};
 //ListView lv;
 public static Boolean flag=true; 
 Boolean flagValue = true;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);


     //Getting the resolution of Tablet
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    System.out.println("Width:"+width);
    System.out.println("Height:"+height);

    //setContentView(R.layout.search_catagories);
        if(width==600 && height==1024)
               {
                   setContentView(R.layout.search_catagories);

                      System.out.println("HIiiiiiiiiiiiiii");


               }
        else{
                    flag = false;
                    setContentView(R.layout.search_categories_ls);
            }

        arraylists_clear();

    TextView txt_search = (TextView)findViewById(R.id.editText1);
    if(flagValue){
    if(SearchRefine.checkflag){
    bundle = getIntent().getExtras();
    keyword = bundle.getString("keyword");
    }
    image_sm.clear();
    title.clear();
    min_price.clear();
     //Opens or Create a database and Table
    db = openOrCreateDatabase("recentsearches1.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
    db.setVersion(1);
    db.setLocale(Locale.getDefault());
    db.setLockingEnabled(true);



    final String DATA="create table if not exists new_searchtab1("+"id INTEGER PRIMARY KEY AUTOINCREMENT,"+"search TEXT UNIQUE);";
    db.execSQL(DATA);
    //=================End

    ContentValues values = new ContentValues();
       values.put("search", keyword);
       long id1 = db.insert("new_searchtab1", null, values);

       db.close();

    }   
    //ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>();

    final ListView lv = (ListView)findViewById(R.id.search_listView);

    final ProgressDialog dialog = ProgressDialog.show(this, "BIZRATE", 



            "Loading....", true);
            final Handler handler = new Handler() {



               public void handleMessage(Message msg) {


                   //LoadEffAdapter();
                   lv.setAdapter(new EfficientAdapter(Search_List_Activity.this));
                   dialog.dismiss();
                  }
               };



            Thread checkUpdate = new Thread() {  



               public void run() {
            //
            // YOUR LONG CALCULATION (OR OTHER) GOES HERE
            //
                   SearchResult_Json.json_parsing();
                  handler.sendEmptyMessage(0);



                  }
               };
            checkUpdate.start();
    //final SearchResult_Json obj = new SearchResult_Json();
    ImageButton grid_view_btn = (ImageButton)findViewById(R.id.gridview_bttn);


    //Categories button
    Button categories_btn = (Button)findViewById(R.id.categories_bttn);
    categories_btn.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            Category_Search_Json.Json_parsing();
            showDialog(CATEGORIES_PRODUCE_ID);
        }
    });

    //Refine Button
    Button btn_refine = (Button)findViewById(R.id.refine_bttn);

    btn_refine.setOnClickListener(new View.OnClickListener() {



        public void onClick(View arg0) {


            //showDialog(SearchRefine.DIALOG_PRODUCE_ID);
            // TODO Auto-generated method stub
        Intent intent = new Intent(Search_List_Activity.this,SearchRefine.class);
        intent.putExtra("keyword",keyword);//Loads Refine Search Screen
        startActivity(intent);
        }

    });

    //Grid Button
    grid_view_btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //obj.json_parse();
            Intent intent = new Intent(Search_List_Activity.this,Search_GridView_Activity.class);
            intent.putExtra("keyword", bundle.getString("keyword"));
            startActivity(intent);
        }
    });


    txt_search.setText(keyword);
    keyword = keyword.replaceAll(" ", "+");
    System.out.println("The String:"+keyword);



    lv.setBackgroundColor(Color.WHITE);
    lv.setCacheColorHint(Color.WHITE); 


//List View On item click
    lv.setOnItemClickListener(new OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent,View view,int position,long idddd)
        {

            //To check the offer and products
            if(type.get(position).equals("OFFER"))
            {
                prod_id = offer_id.get(position);
            Intent intent = new Intent(Search_List_Activity.this,Compare_Product_Activity.class);
            intent.putExtra("productType", "SZOID");
            startActivity(intent);
            }
            else
            {

                prod_id = offer_id.get(position);
                Intent intent = new Intent(Search_List_Activity.this,Compare_Product_Activity.class);
                intent.putExtra("productType", "SZPID");
                startActivity(intent);
            }

        }
    });
}



private void arraylists_clear() {
    // TODO Auto-generated method stub
    type.clear();
    image_sm.clear();
    title.clear();
    min_price.clear();
    des.clear();
    manu.clear();
    urls.clear();
    image_me.clear();
    image_la.clear();
    image_ex.clear();
    rel.clear();
    id.clear();
    cat_id.clear();
    min_price.clear();
    max_price.clear();
    stores.clear();
    offer_sku.clear();
    offer_detailurl.clear();
    offer_price.clear();
    offer_ori_price.clear();
    offer_markdownperct.clear();
    offer_shipcost.clear();
    offer_tax.clear();
    offer_total_price.clear();
    offer_bidded.clear();
    offer_merchant_prod_id.clear();
    offer_merchant_name.clear();
    offer_merchant_rating_img.clear();
    offer_merchant_logo.clear();
    offer_condition.clear();
    offer_stock.clear();
    offer_ship_amt.clear();
    offer_ship_type.clear();
    offer_id.clear();
    offer_cat_id.clear();
    offer_merchant_id.clear();
    category_id.clear();
    category_name.clear();
    offer_rating.clear();


}


/*protected void LoadEffAdapter() {
    // TODO Auto-generated method stub
    lv.setAdapter(new EfficientAdapter(this));

}
*/

//Loading of Images, title, min_price

//Efficient Adapter
public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return image_sm.size();
    }


    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        try {

            url1 = new URL(image_sm.get(position));
            url2 = new URL(image_me.get(position));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {


            if(flag){
            bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
            }
            else{
                bmp = BitmapFactory.decodeStream(url2.openConnection().getInputStream());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //holder.img.setImageBitmap(bmp);
        if (convertView == null) {


            if(flag){
            convertView = mInflater.inflate( R.layout.search_catagory_row, null);
            System.out.println("WITH PORTRAITTT");
            }
            else{
                convertView = mInflater.inflate( R.layout.search_category_row_land, null);
                System.out.println("WITH LANDSCAPEEE");
            }
            holder = new ViewHolder();
            holder.text = (TextView) convertView
            .findViewById(R.id.search_text);
            holder.text2 = (TextView) convertView
            .findViewById(R.id.text_view_2);
            holder.img = (ImageView) convertView.findViewById(R.id.images);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }


        holder.text.setText(title.get(position));
        holder.text2.setText(min_price.get(position));
        holder.img.setImageBitmap(bmp);
        final int pos = position;


        return convertView;
    }

    static class ViewHolder {
        TextView text;
        TextView text2;
        ImageView img;

    }
}


 @Override
    protected Dialog onCreateDialog(int id) {

        switch(id) {


       case CATEGORIES_PRODUCE_ID:
          dialog_category = new Dialog(this);
         dialog_category.setContentView(R.layout.category_dialog);
            ListView category_list= (ListView)dialog_category.findViewById(R.id.list_cat);
           category_list.setBackgroundColor(Color.WHITE);
          category_list.setCacheColorHint(Color.WHITE);
            category_list.setAdapter(new ArrayAdapter<String>(this,R.layout.cat_row, R.id.label , category_name));

            // List view click

           category_list.setOnItemClickListener(new OnItemClickListener() {


                public void onItemClick(AdapterView<?> parent,View view,int position,long idddd)
                {

                    //To check the offer and products
                    cat_flag=true;
                    SearchRefine.checkflag=false;
                    Intent intent = new Intent(Search_List_Activity.this,Search_List_Activity.class);
                    intent.putExtra("keyword",keyword);
                    category_get_id=category_id.get(position);
                    System.out.println("CATEGORY NAME-->"+category_get_id);
                    startActivity(intent);

                }
            });

           return dialog_category;  

        }
        return null;
    }    
}

它给出了以下例外。

06-07 16:39:12.660: ERROR/AndroidRuntime(986): FATAL EXCEPTION: main
06-07 16:39:12.660: ERROR/AndroidRuntime(986): java.lang.IllegalArgumentException: View not attached to window manager
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.app.Dialog.dismissDialog(Dialog.java:278)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.app.Dialog.access$000(Dialog.java:71)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.app.Dialog$1.run(Dialog.java:111)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.app.Dialog.dismiss(Dialog.java:268)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at org.bizrate.tablet.Search_List_Activity$1.handleMessage(Search_List_Activity.java:174)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.os.Looper.loop(Looper.java:123) 
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at java.lang.reflect.Method.invoke(Method.java:521)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-07 16:39:12.660: ERROR/AndroidRuntime(986):     at dalvik.system.NativeStart.main(Native Method)

大家好我想在这里添加一件事,当我从这段代码中删除线程代码(进度条代码)时,它运行得很好。但我必须实施进度条。 请帮我。 谢谢你们。

1 个答案:

答案 0 :(得分:5)

之前已经回答了好几次...... 这是因为当您更改方向时,活动将再次创建。

要处理此问题,请在清单文件的“活动”中使用以下代码。

android:configChanges="keyboardHidden|orientation"