刷新崩溃的 Android WebView
Refreshing a crashed Android WebView
我有一个 WebView
我用 setContentView
换了一个 EditText
。 EditText
用于修改 Javascript 来源。 WebView
使用 loadView
渲染包裹在 HTML "script"
标签中的 Javascript 源代码。我的问题是在尝试 运行 错误的 Javascript 代码后,我无法刷新 WebView
。我试过 refresh()
方法,我什至试过实例化一个全新的 WebView
;所以也许我误解了这个问题。我的应用程序如何从 运行 错误的脚本中恢复?
我原本认为源代码是不必要的;但因为它很短:
public class MainActivity extends Activity {
boolean render = true;
EditText text;
WebView html;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = new EditText(this);
text.setGravity(Gravity.TOP);
text.setTypeface(Typeface.MONOSPACE);
text.setBackgroundColor(Color.WHITE);
text.setTextColor(Color.BLUE);
text.setTextSize(14);
html = new WebView(this);
html.getSettings().setJavaScriptEnabled(true);
load();
//show();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setTitle("");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
render = true;
break;
case 1:
render = false;
break;
}
show();
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (tab.getPosition() == 0)
html.reload();
}
};
actionBar.addTab(
actionBar.newTab()
.setText("render")
.setTabListener(tabListener));
actionBar.addTab(
actionBar.newTab()
.setText("source")
.setTabListener(tabListener));
}
public void onBackPressed() {
return;
}
void show() {
runOnUiThread(new Runnable() {
public void run() {
if (!render) {
setContentView(text);
} else {
String page = "<script language=\"javascript\">" + text.getText().toString() + "</script>" +
"<body bgcolor=\"#ffff00\" text=\"#444444\" />";
html.loadData(page, "text/html", Charset.defaultCharset().displayName());
setContentView(html);
}
}
});
}
String filename = "hypertext.dat";
void save() {
try {
FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(text.getText().toString().getBytes());
outputStream.close();
} catch (Exception e) {
// -
}
}
void load() {
try {
FileInputStream inputStream = openFileInput(filename);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
final StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
r.close();
inputStream.close();
runOnUiThread(new Runnable() {
public void run() {
text.setText(total.toString());
}
});
} catch (Exception e) {
// -
}
}
public void onPause() {
save();
super.onPause();
}
public void onDestroy() {
save();
super.onDestroy();
}
public void onRestart() {
super.onRestart();
load();
}
}
将 System.exit(0);
添加到 void onDestroy() { }
会导致 WebView
充分刷新();
我有一个 WebView
我用 setContentView
换了一个 EditText
。 EditText
用于修改 Javascript 来源。 WebView
使用 loadView
渲染包裹在 HTML "script"
标签中的 Javascript 源代码。我的问题是在尝试 运行 错误的 Javascript 代码后,我无法刷新 WebView
。我试过 refresh()
方法,我什至试过实例化一个全新的 WebView
;所以也许我误解了这个问题。我的应用程序如何从 运行 错误的脚本中恢复?
我原本认为源代码是不必要的;但因为它很短:
public class MainActivity extends Activity {
boolean render = true;
EditText text;
WebView html;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = new EditText(this);
text.setGravity(Gravity.TOP);
text.setTypeface(Typeface.MONOSPACE);
text.setBackgroundColor(Color.WHITE);
text.setTextColor(Color.BLUE);
text.setTextSize(14);
html = new WebView(this);
html.getSettings().setJavaScriptEnabled(true);
load();
//show();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setTitle("");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
render = true;
break;
case 1:
render = false;
break;
}
show();
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
// hide the given tab
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (tab.getPosition() == 0)
html.reload();
}
};
actionBar.addTab(
actionBar.newTab()
.setText("render")
.setTabListener(tabListener));
actionBar.addTab(
actionBar.newTab()
.setText("source")
.setTabListener(tabListener));
}
public void onBackPressed() {
return;
}
void show() {
runOnUiThread(new Runnable() {
public void run() {
if (!render) {
setContentView(text);
} else {
String page = "<script language=\"javascript\">" + text.getText().toString() + "</script>" +
"<body bgcolor=\"#ffff00\" text=\"#444444\" />";
html.loadData(page, "text/html", Charset.defaultCharset().displayName());
setContentView(html);
}
}
});
}
String filename = "hypertext.dat";
void save() {
try {
FileOutputStream outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(text.getText().toString().getBytes());
outputStream.close();
} catch (Exception e) {
// -
}
}
void load() {
try {
FileInputStream inputStream = openFileInput(filename);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
final StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line + "\n");
}
r.close();
inputStream.close();
runOnUiThread(new Runnable() {
public void run() {
text.setText(total.toString());
}
});
} catch (Exception e) {
// -
}
}
public void onPause() {
save();
super.onPause();
}
public void onDestroy() {
save();
super.onDestroy();
}
public void onRestart() {
super.onRestart();
load();
}
}
将 System.exit(0);
添加到 void onDestroy() { }
会导致 WebView
充分刷新();