开发者

Webview Problem in android emulator

开发者 https://www.devze.com 2023-02-16 10:21 出处:网络
I am having difficulty in opening a webview within my application. I can o开发者_JAVA技巧pen it within other applications but not the one I want.

I am having difficulty in opening a webview within my application.

I can o开发者_JAVA技巧pen it within other applications but not the one I want.

Also I can only open up webpages using

startActivity(new Intent(
    Intent.ACTION_VIEW,
    Uri.parse("http://www.google.com")));

I am not sure why I cannot open webviews, I need this to work as I have a view within my application.

The way I am opening the webview is

setContentView(R.layout.webview);
WebView wv = (WebView)findViewById( R.id.link4_view );          
wv.loadUrl("http://www.google.com");

I do not receive any error in logcat, just says web page not available.

Thanks


Have you put internet permission into your AndroidManifest.xml file?

It should look like:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" .... >
...
    <uses-permission android:name="android.permission.INTERNET" />
...
</manifest>


The only way I could get this to work properly was by taking out the permission then loading the application. Then closing it and re-adding the permission and it worked :)


Sample Simple WebView Activity

Java Class Activity File - Code Snippet

setContentView(R.layout.main);
WebView MyWebView = (WebView) findViewById(R.id.webkit);        
MyWebView.getSettings().setJavaScriptEnabled(true);
MyWebView.loadUrl("http://www.stackoverflow.com");

XML Layout File

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <WebView android:id="@+id/webkit" android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:scrollbars="none" />
</ScrollView>

And Make Sure You Have Added Permission To Your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


Sometimes WebView is not working on emulator running on operating system due to internet sharing problem. You can try it by running on external android device.

0

精彩评论

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

关注公众号