我是Android新手,开始使用基本应用程序(选择API 5.1 /创建虚拟设备5.1.1/API22,因为这是我将在以后使用的实际情况),只包含WebView :Android WebView保持空白
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
然而,无论我在活动的Java代码做的页面保持空白。我尝试了很多从SO等tipps,但没有任何工作,所以它一定是我很想念的东西?
MainActivity.java
package de.jstest.notes;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView myWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setDomStorageEnabled(true); // is this actually needed?
webSettings.setJavaScriptEnabled(true);
String summary = "<html><head><title>WebView JS Tester</title></head><body>" +
"Code<br>" +
"<textarea id='code'></textarea><br>" +
"Result<br>" +
"<textarea id='result'></textarea><br>" +
"<input type='button' value='Eval' onclick='" +
"document.getElementById(\"result\").value=eval(document.getElementById(\"code\").value)" +
"'>" +
"</body></html>";
myWebView.loadData(summary, "text/html; charset=utf-8", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("WebView 'title' = " + myWebView.getTitle());
builder.setPositiveButton("Now try a web page...",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
myWebView.loadUrl("https://www.google.de/");
dialog.dismiss(); // otherwise, dialog stays open
}
}
);
}
}
此外,对话框(按钮)不工作(既不是我上面的代码,也没有更多的基本方法直接设置单击处理),即,应用程序打开(有时与,有时没有其标题“注释”...),并描述WebView保持空白;我看到对话框(说网页视图title
为空),但点击一个按钮(“现在尝试网页...”)完全没有影响。点击几次后,我被问到是否要关闭没有回应的应用程序...
请注意,loadData
和loadUrl
都会导致空白页。对于后者的工作,我想
AndroidManifest.xml中
<uses-permission android:name="android.permission.INTERNET" />
但这并没有改变任何东西。
'loadData'部分似乎在这里工作OK ...我的版本(对不起)777e28a https://github.com/rdp/sensible-cinema – rogerdpack
谢谢!我今晚会尝试并提供反馈。 – SomeDev