I am using the following code. When i change orientation from portrait to landscape, and then again to portrait. it gives Exception "view not attached to window manager". how to solve this problem. thank you.
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);
}开发者_Go百科
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;
}
}
It Gives the following exception.
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)
Hi All I want add one More thing here, when I remove the thread code(Progress bar code) from this code.it is running perfectly. But I have to implement with progress bar. please help me. Thank you all.
This has been answered several time before... This is because when you change your orientation the Activity is getting created again .
To handle this use th following code in your Activity in your manifest file.
android:configChanges="keyboardHidden|orientation"
精彩评论