在Android中旋转主(活动)屏幕时关闭应用程序

时间:2014-12-03 07:01:33

标签: android android-layout android-orientation

我正在制作两个布局,一个用于风景,另一个用于肖像。

为横向制作layout-land文件夹并放置所有横向布局,所有横向布局都正常工作但是当我在主要活动上旋转时,它会关闭。

已移除layout-land文件夹,然后尝试再次关闭应用

主要活动

   <activity
        android:name=".UnsignPropertyAlert"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

UnsignPropertyAlert类

 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.unsignalert);

            Commons.setContext(context);
            listView =(ListView) findViewById(R.id.list);
          //  listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

         btnregister= (TextView) findViewById(R.id.login);

         btnNext= (Button) findViewById(R.id.btnnext);
         btnPrev= (Button) findViewById(R.id.btnprev);

        AdView adView = (AdView)this.findViewById(R.id.adView);

        //adView.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("TEST_DEVICE_ID")
            .build();
        adView.loadAd(adRequest);


        Bundle bundle = new Bundle();
        bundle.putString("color_bg","#fdfbfc");
        bundle.putString("color_text", "#81BE32");
        AdMobExtras extras = new AdMobExtras(bundle);
         adRequest = new AdRequest.Builder()
            .addNetworkExtras(extras)
            .build();

        // btnNext.setVisibility(View.INVISIBLE);
       //   btnPrev.setVisibility(View.INVISIBLE);

         btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                nextProp();
            }
        });

         btnPrev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                prevProp();
            }
        });

        login = new File("/data/data/com.dwellesque/shared_prefs/Login.xml");
        if(Commons.HaveNetworkConnection(context))
        {
        if(login.exists())
        {
            btnregister.setText("LOGIN");
             preferences=getSharedPreferences("Login", MODE_PRIVATE);
            String zip= preferences.getString("ZIP", "");

           url="http://www.......";
          backurl="http://www.....";
        }
        else
        {
             url="http://www......";
            backurl="http://www.......";
        }


            pd=new ProgressDialog(UnsignPropertyAlert.this,ProgressDialog.THEME_HOLO_LIGHT);
            pd.setMessage("Loading....");
            pd.setTitle(null);

            pd.show();
              url=url.replace(" ", "%20");
              Log.d("First", url);
              JsonRequestAsycn task=new JsonRequestAsycn();
             task.execute(url);
             task.delegate=UnsignPropertyAlert.this;
        }
        else
        {
            Toast.makeText(context, "Please check your internet connection! ", Toast.LENGTH_LONG).show();
        }

                btnSearch= (TextView) findViewById(R.id.search);
                btnSearch.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity(new Intent(UnsignPropertyAlert.this,Main_Serach.class));
                    }
                });


        btnregister.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(btnregister.getText().toString().equals("LOGIN"))
                {
                    startActivity(new Intent(UnsignPropertyAlert.this,Login.class));
                }
                else
                {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www......."));
                    startActivity(browserIntent);
                }

            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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


                String url = arlisturl.get(position);

                Log.d("URL", url);

                if (!url.startsWith("http://") && !url.startsWith("https://"))
                       url = "http://" + url;

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i); 
            }
            });  


        listView.setOnTouchListener(new OnSwipeTouchListener(context) {
            @Override
            public void onSwipeLeft() {
                // Whatever
                nextProp();
                // Toast.makeText(context, "left", Toast.LENGTH_LONG).show();
            }
            public void onSwipeRight() {
                // Whatever
                prevProp();
                // Toast.makeText(context, "Right", Toast.LENGTH_LONG).show();
            }
        });

}

    @Override
    public void processFinish(String output) {
        pd.dismiss();
        // TODO Auto-generated method stub
        if(output.trim().contains("Result not found !") || output.length()==114 || output.length()<1)
        {
             Toast.makeText(context, "Property Not Found", Toast.LENGTH_LONG).show();

        }
        else if(output.trim().contains("TimeOut"))
        {
             Toast.makeText(context, "Connection Timeout!", Toast.LENGTH_LONG).show();
        }
        else
        {
            showdata(output,listView,true);

        }


    }



    // Set Data in List View

    void showdata(String data,ListView lv,Boolean first)
    {

         doc = XMLfunctions.XMLfromString(data);
         NodeList nodes = doc.getElementsByTagName("PROPERTY");
            for (int i = 0; i<nodes.getLength(); i++) {
                Element e = (Element) nodes.item(i);

                // Street Name

                if (!("null").equals(XMLfunctions.getValue(e, "street"))) {
                    arliststitname.add(XMLfunctions.getValue(e, "street"));


                } else {
                    arliststitname.add(" ");
                }

                // Location

                if (!("null").equals(XMLfunctions.getValue(e, "city"))) {
                    if(!("null").equals(XMLfunctions.getValue(e, "state")))
                    {
                        arlistlocation.add(XMLfunctions.getValue(e, "city")+" ,"+XMLfunctions.getValue(e, "state"));
                    }
                     arlistlocation.add(XMLfunctions.getValue(e, "city"));

                } else {
                    arlistlocation.add(" ");
                }

                // Square Footage

                if (!("null").equals(XMLfunctions.getValue(e, "SquareFootage"))) {
                    arlistsqare.add(XMLfunctions.getValue(e, "SquareFootage"));

                } else {
                    arlistsqare.add(" ");
                }

           // price

                if (!("null").equals(XMLfunctions.getValue(e, "price"))) {
                    arlistprice.add(XMLfunctions.getValue(e, "price"));

                } else {
                    arlistprice.add(" ");
                }

                // Images

                if (!("null").equals(XMLfunctions.getValue(e, "picture"))) {
                    String[] imageUrls=XMLfunctions.getValue(e, "picture").split("\\|\\|");
                    arlistimg.add(imageUrls[0]);

                } else {
                     arlistimg.add("");

                }
                // posted on
                if (!("null").equals(XMLfunctions.getValue(e, "EnteredDate"))) {
                    arlistposted.add(XMLfunctions.getValue(e, "EnteredDate"));
                } else {
                    arlistposted.add("");

                }
                if (!("null").equals(XMLfunctions.getValue(e, "description"))) {
                    arlistDesc.add(XMLfunctions.getValue(e, "description"));
                } else {
                    arlistDesc.add("");

                }
                if (!("null").equals(XMLfunctions.getValue(e, "propertystatus"))) {
                    arlistproperty.add(XMLfunctions.getValue(e, "propertystatus"));
                } else {
                    arlistproperty.add("");

                }
                if (!("null").equals(XMLfunctions.getValue(e, "EnteredDate"))) {
                    arlistyear.add(XMLfunctions.getValue(e, "EnteredDate"));
                } else {
                    arlistyear.add("");

                }
                //URL
                if (!("null").equals(XMLfunctions.getValue(e, "propertyurl"))) {
                    arlisturl.add(XMLfunctions.getValue(e, "propertyurl"));
                } else {
                    arlisturl.add("");

                }
                // ID
                if (!("null").equals(XMLfunctions.getValue(e, "id"))) {
                    arlistID.add(XMLfunctions.getValue(e, "id"));
                } else {
                    arlistID.add("");

                }
            }
            if(first)
            {
                lv.setAdapter(null);
                  String[] Street={ arliststitname.get(0) };
                 adapter= new 
                        AlertListAdapter(context,arlistimg.get(0), Street, arlistlocation.get(0),
                                arlistsqare.get(0), arlistprice.get(0),arlistposted.get(0),arlisturl.get(0),arlistID.get(0));
                lv.setAdapter(adapter);

            }


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(login.exists())
        {
            btnregister.setText("LOGIN");
        }
    }


    @Override
    protected void onDestroy() {
    // closing Entire Application
    android.os.Process.killProcess(android.os.Process.myPid());
    Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
    editor.clear();
    editor.commit();
    trimCache(this);
    super.onDestroy();
    }


    public static void trimCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);

        }
    } catch (Exception e) {
        // TODO: handle exception
    }
    }


    public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
            else
            return true;
        }
    }
    return false;

    }

     @Override
     public void onBackPressed() {
      //  Log.d("CDA", "onBackPressed Called");
        Intent setIntent = new Intent(Intent.ACTION_MAIN);
        setIntent.addCategory(Intent.CATEGORY_HOME);
        setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(setIntent);
     }

     void nextProp()
     {
         if(Commons.HaveNetworkConnection(context))
         {
             if(proppos<arliststitname.size()-2)
             {
             btnPrev.setEnabled(true);
             proppos++;
          listView.setAdapter(null);
          String[] Street={ arliststitname.get(proppos) };
          adapter= new 
                AlertListAdapter(context,arlistimg.get(proppos), Street, arlistlocation.get(proppos),
                        arlistsqare.get(proppos), arlistprice.get(proppos),arlistposted.get(proppos),arlisturl.get(proppos),arlistID.get(proppos));
          listView.setAdapter(adapter);
         }
        else
         {
            start=start+10;
            String newurl=backurl+"&start="+start;
            Log.d("URL", newurl);
             Backgrounddata getData= new Backgrounddata();
             getData.execute(newurl);
         }
         }

     }

     void prevProp()
     {
         if(Commons.HaveNetworkConnection(context))
         {
         if(proppos<=0)
         {
             btnPrev.setEnabled(false);
         }
         else
         {
        proppos--;
         listView.setAdapter(null);
          String[] Street={ arliststitname.get(proppos) };
         adapter= new 
                AlertListAdapter(context,arlistimg.get(proppos), Street, arlistlocation.get(proppos),
                        arlistsqare.get(proppos), arlistprice.get(proppos),arlistposted.get(proppos),arlisturl.get(proppos),arlistID.get(proppos));
        listView.setAdapter(adapter);
         }
         }
     }


     // background hit data

     private class Backgrounddata extends AsyncTask<String, Void, String> {

            @Override
                protected String doInBackground(String... urls) {
                  String response = "";
                  HttpEntity resEntity;
                    try {

                           for (String url : urls) {
                        // Create the client
                             DefaultHttpClient client = new DefaultHttpClient();
                            HttpGet httpGet = new HttpGet(url);
                        // Execute HTTP Post Request and get response
                        // Set connection timeout
                        int timeoutConnection = 15000 ;
                        HttpConnectionParams.setConnectionTimeout(httpGet.getParams(), timeoutConnection);
                        // set socket timeout
                        int timeoutSocket = 15000 ;
                        HttpConnectionParams.setSoTimeout(httpGet.getParams(), timeoutSocket);
                        HttpResponse responsePOST = client.execute(httpGet);
                        resEntity = responsePOST.getEntity();
                        response=EntityUtils.toString(resEntity);
                           }
                    } catch (Exception e) {
                         return "TimeOut";
                     // e.printStackTrace();
                    }

                  return response;
                }

                @SuppressLint("DefaultLocale")
                @Override
                protected void onPostExecute(String result) {
                    pd.dismiss();
                    try{
                        showdata(result,listView,false);
                    }
                    catch(Exception e)
                    {
                        Log.d("error hai",""+e);
                    }

                }

                @Override
                protected  void onPreExecute()
                {
                     pd=new ProgressDialog(UnsignPropertyAlert.this,ProgressDialog.THEME_HOLO_LIGHT);
                     pd.setMessage("Loading....");
                     pd.setTitle(null);
                     pd.show();

                }
        }

请帮助我如何解决此问题

提前致谢

0 个答案:

没有答案