刷新崩溃的Android WebView

时间:2015-12-05 20:28:52

标签: javascript java android android-webview

我使用WebViewEditText进行setContentView交换。 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();
    }
}

1 个答案:

答案 0 :(得分:1)

System.exit(0);添加到void onDestroy() { }会导致WebView充分刷新();