2011-03-21 209 views
3

我的應用程序出現問題。它正確地通過時間表系統中的所有形式,然後顯示時間表,然後當我旋轉屏幕查看橫向或縱向時,它會返回到第一個屏幕,我無法弄清楚爲什麼...提前感謝任何幫助!Android屏幕旋轉問題

package com.timetable; 

import java.io.FileOutputStream; 
import java.io.IOException; 



    import org.jsoup.Jsoup; 
    import org.jsoup.nodes.Document; 
    import org.jsoup.nodes.Element; 
    import org.jsoup.select.Elements; 

    import android.app.Activity; 
    import android.graphics.Bitmap; 
    import android.graphics.Canvas; 
    import android.graphics.Picture; 
    import android.os.Bundle; 
    import android.view.KeyEvent; 
    import android.view.Window; 
    import android.webkit.WebChromeClient; 
    import android.webkit.WebView; 
    import android.webkit.WebViewClient; 

    public class Timetable extends Activity { 
     WebView mWebView; 
     //int count = 0; 
     String html = "<html><body>"; 
     String html2 = "<html><body>"; 
     Document docs; 
     Document docs2; 
     public String text(String htmlrec) 
     { 
      try { 
       docs2 = Jsoup.connect(htmlrec).get(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Elements data = docs2.getElementsByClass("timetables"); 
      html2 += data.toString()+ "</body></html>"; 
      return html2; 
     } 

     public void main(String... args) { 

      try { 
       docs = Jsoup.connect("http://www.dcu.ie/timetables/search.shtml").get(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      Elements table = docs.getElementsByClass("timetables"); 
      html += table.toString() + "</body></html>"; 
     } 

     public void onCreate(Bundle savedInstanceState) { 
      main(); 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
      mWebView = (WebView) findViewById(R.id.webview); 
      mWebView.setWebViewClient(new TimeClient()); 
      mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
      mWebView.setClickable(true); 
      mWebView.requestFocus(); 
      mWebView.setInitialScale(1); 
      //mWebView.getSettings().setBlockNetworkLoads(true); 
      mWebView.getSettings().setSupportZoom(true); 
      mWebView.getSettings().setJavaScriptEnabled(true); 
      mWebView.getSettings().setDomStorageEnabled(true); 
      mWebView.loadDataWithBaseURL("http://www.dcu.ie/timetables/",html, "text/html", "utf-8","http://www.dcu.ie/timetables/"); 

      final Activity MyActivity = this; 
      mWebView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) 
      { 
       //Make the bar disappear after URL is loaded, and changes string to Loading... 
       MyActivity.setTitle("Loading..."); 
       MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded 

       // Return the app name after finish loading 
       if(progress == 100) 
        MyActivity.setTitle(R.string.app_name); 
       } 
      }); 
     } 

     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
       mWebView.goBack();    
       return true; 
      } 
      return super.onKeyDown(keyCode, event); 
     } 
     private class TimeClient extends WebViewClient { 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       if (view.canGoBack() == false) 
       //if (count == 0) 
       { 
        String newUrl = text(url); 
        view.loadDataWithBaseURL("http://www.dcu.ie/timetables/",newUrl, "text/html", "utf-8","http://www.dcu.ie/timetables/"); 
        // count+ 

+; 
      } 
      else 
      { 

       view.loadUrl(url); 
       Picture picture = view.capturePicture(); 
       Bitmap b = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Bitmap.Config.ARGB_8888); 
       Canvas c = new Canvas(b); 
       picture.draw(c); 
       FileOutputStream fos = null; 
       try 
       { 
        fos = new FileOutputStream("/sd/dcu" + 
        System.currentTimeMillis() + ".jpg"); 
        if (fos != null) 
        { 
         b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
         fos.close(); 
        } 
       } 
       catch(Exception e) 
       { 
       } 
      } 

      return true; 
     } 
    } 


} 

回答

6

發生了什麼事情是,當你改變方向時,活動被破壞和重新創建。爲了避免活動破壞並重新創建,提出:

android:configChanges="orientation|keyboardHidden|keyboard" 

AndroidManifest.xml一個你的活動屬性。

+0

工作!謝謝! :) – dbaby7 2011-03-21 03:18:17