某些类型的网址未在我的应用程序中加载,但可以在设备浏览器中加载。以下是我的代码中没有工作的Url示例 - “http://apps.takeyourapp.com/testApp/staging/index.html”
package com.example.webviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
WebView webViewPlaceholder;
String URL = "http://apps.takeyourapp.com/testApp/staging/index.html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webViewPlaceholder = (WebView) findViewById(R.id.webholder);
webViewPlaceholder.getSettings().setJavaScriptEnabled(true);
webViewPlaceholder
.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webViewPlaceholder.setScrollbarFadingEnabled(true);
webViewPlaceholder.getSettings().setLoadsImagesAutomatically(true);
webViewPlaceholder.getSettings().setUseWideViewPort(true);
webViewPlaceholder.loadUrl(URL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
创建WebViewClient
并加载url
,如
public class myWebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
setWebViewClient
喜欢:
webView.setWebViewClient(new myWebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
并在INTERNET
manifest.xml
权限
<uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:0)
尝试此解决方案。
public class Main extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://apps.takeyourapp.com/testApp/staging/index.html");
setContentView(mWebview );
}
}