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;
}
}
}
I have a problem with my app. It correctly goes through all forms in a timetable system and shows the timetable however when I then rotate the screen to view landscape or portrait depending, it goes back to the first screen and I cant figure out why...thanks in advance for any help!
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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的情况是,当您改变方向时,活动将被销毁并重新创建。为了避免 Activity 被销毁并重新创建,请将:
作为 Activity 的属性放入
AndroidManifest.xml
中。What's happening is that when you change orientation the Activity is destroyed and recreated. To avoid having the activity destroyed and recreated, put:
as an attribute of your activity in
AndroidManifest.xml
.