开发者

Android - Webview in pop up dialog?

I am trying to get a webview into a popup dialog. I can get it to pop up with the webview, but when I try to make it load a Url, it crashes my app... Any help? I have also tried putting the webview settings in OnCreate.

case R.id.calculatorBtn:
        Dialog dialog = new Dialog(DrawingActivity.this);
        dialog.setContentView(R.layout.webviewdialog);
        dialog.setTitle("Calculator");
        dialog.setCancelable(true);
        dialog.show();

IT WORKS FINE UNTIL I ADD THE STUFF BELOW THIS LINE.

         webview = (WebVie开发者_Go百科w) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
       webview.loadUrl("http://myurl.com/hi.html");

        break;


Try replacing:

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

with:

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

Also, usually, the first thing you do when your app crashes is you look in logcat for an exception and try to figure out what is wrong from it. If you can't and end up posting a question on Stack Overflow, that exception is very useful for us, in order to help you.

I bet the exception you're getting is an NullPointerException on this line:

webview.getSettings().setJavaScriptEnabled(true);


The may be in initializing the value of webview.

Try with the following code

Dialog dialog = new Dialog(DrawingActivity.this);
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View vi = inflater.inflate(R.layout.webviewdialog, null);
            dialog.setContentView(vi);
            dialog.setTitle("Calculator");
            dialog.setCancelable(true);
            WebView wb = (WebView) vi.findViewById(R.id.webview);
            wb.loadUrl("http://developer.android.com/guide/topics/ui/dialogs.html");
            System.out.println("..loading url..");
            dialog.show();


Could you please supply more code (maybe the entire method/class) so we can see the context in which this is placed. There is a good chance that the previous code is causing this part to crash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜