1

I already have a WebView with content loaded and I need to select text that will open a new WebView in a popup window. The popup will contain a form which I will submit and when I hit enter it will save data and close the popup. At this stage, I need help in opening the WebView popup by selecting text or clicking button.

I have tried the answer from https://stackoverflow.com/a/9173368/2341601 but this crashes the app when I do

WebView wv = new WebView(this);

LogCat message:

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)

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

Community
  • 1
  • 1
skzi
  • 340
  • 3
  • 14

2 Answers2

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>
Vaishali Sutariya
  • 5,093
  • 30
  • 32
0

Avoid request future crash.

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();
}
}
raja
  • 368
  • 2
  • 13
  • Thanks but the application is crashing in the last line - df.show(getSupportFragmentManager(), "dialog"); – skzi Sep 25 '14 at 07:14
  • 1
    May be android-support-v7-appcompat.jar is missing from your libs folder(android.support.v4.app.FragmentActivity.getSupportFragmentManager()). post your logcat also.... – raja Sep 25 '14 at 08:19
  • The support libraries are not in the libs folder, however, I have these defined in build.gradle file and the relevant xml files are also present in .idea\libraries folder. I checked the SDK manager and the support libraries are installed. – skzi Sep 26 '14 at 01:14