Android:如何在弹出窗口中打开WebView?

时间:2014-09-25 05:02:50

标签: android webview

我已经加载了内容的WebView,我需要选择将在弹出窗口中打开新WebView的文本。弹出窗口将包含我将提交的表单,当我点击输入时,它将保存数据并关闭弹出窗口。在这个阶段,我需要帮助打开WebView弹出窗口,方法是选择文本或单击按钮。

我已经尝试了https://stackoverflow.com/a/9173368/2341601的答案,但是当我这样做时会崩溃应用程序

WebView wv = new WebView(this);

LogCat消息:

Process: com.example.user.testapp, PID: 13984
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.testapp/com.example.user.testapp.MainActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
        at android.app.ActivityThread.access$900(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5653)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

引起:android.util.AndroidRuntimeException:在添加内容之前必须调用requestFeature()

2 个答案:

答案 0 :(得分:0)

final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.webview_layout);

WebView wv = (WebView) dialog
            .findViewById(R.id.webview);

wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        return true;
    }
});

dialog.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode,
                KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {

                dialog.dismiss();
            }

            return false;
        }
    });

webview_layout.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

答案 1 :(得分:0)

避免请求将来崩溃。

DialogFragment.java

public class DialogFragment extends android.support.v4.app.DialogFragment {

private boolean isModal = false;

public static DialogFragment newInstance()
{
    DialogFragment frag = new DialogFragment();
    frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
    return frag;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    if(isModal) // AVOID REQUEST FEATURE CRASH
    {
    return super.onCreateView(inflater, container, savedInstanceState);
    }
    else
    {
    View view = inflater.inflate(R.layout.dialog_webview, container, false);
    setupUI(view);
    return view;
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater

    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_webview, null);

    builder.setView(view);
    WebView wv = (WebView) view.findViewById(R.id.webView);

    wv.loadUrl("http:\\www.google.com");
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }
    });
    return builder.create();
}
}