Android WebView Activity 启动然后在 5.1.1 上崩溃
Android WebView Activity Starts Then Crashes On 5.1.1
好吧,这对你们来说很奇怪。我有一个非常简单的 android 应用程序,其中包含两个 webview 活动。他们(显然)都查看了我服务器上托管的 html 网页。这就是问题所在;该应用程序 运行 在我的 6.0.1 设备上完全正常,但是当我 运行 它在我的 5.1.1 电视盒上时,它正常启动,文本输入字段显示闪烁的指示器,所以我知道它起来 运行ning。指示灯闪烁3、4次后应用强制关闭
Manifest.xml
package="myapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.screen.landscape" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.type.television" android:required="false" />
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
<uses-feature android:name="android.hardware.wifi" android:required="false" />
<uses-feature android:name="android.software.leanback" android:required="false"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault"
android:banner="@drawable/splash">
<activity
android:name="myapp.ShowWebView"
android:finishOnTaskLaunch="true"
android:label="@string/chat_name"
android:theme="@android:style/Theme.DeviceDefault"
android:excludeFromRecents="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="myapp.ShowWebViewTerms"
android:finishOnTaskLaunch="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:excludeFromRecents="false" />
</application>
以及不断失败的activity:
public class ShowWebView extends Activity {
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This will not show title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_web_view);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
if(haveNetworkConnection()){
startWebView("http://mywebsite/chat/index.php");
} else {
webView.loadUrl("error.html");
}
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean Override(WebView view, String url) {
view.loadUrl(url);
return true;
}
//If url has "tel:245678" , on clicking the number it will directly call to inbuilt calling feature of phone
public boolean shouldOverrideUrlLoading(WebView view ,String url){
if(url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
} else {
view.loadUrl(url);
}
return true; }
//Show loader on url load
});
// Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
//Additional Webview Properties
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLayoutAlgorithm(webView.getSettings().getLayoutAlgorithm().NORMAL);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(false);
webView.setSoundEffectsEnabled(true);
webView.setHorizontalFadingEdgeEnabled(false);
webView.setKeepScreenOn(true);
webView.setScrollbarFadingEnabled(true);
webView.setVerticalFadingEdgeEnabled(false);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
finish();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
不幸的是,因为我 运行 在电视盒上遇到这个错误,我无法针对实际问题进行调试。这可能是一个远景,但我是 android 开发的新手,也许我会错过一些你不会错过的东西。非常感谢任何帮助或提示。提前致谢!
启用 WiFi ADB 后,我发现清单中缺少这个
uses-permission android:name="android.permission.WAKE_LOCK"
如果有人遇到相同或类似的问题,将把它留在此处。
好吧,这对你们来说很奇怪。我有一个非常简单的 android 应用程序,其中包含两个 webview 活动。他们(显然)都查看了我服务器上托管的 html 网页。这就是问题所在;该应用程序 运行 在我的 6.0.1 设备上完全正常,但是当我 运行 它在我的 5.1.1 电视盒上时,它正常启动,文本输入字段显示闪烁的指示器,所以我知道它起来 运行ning。指示灯闪烁3、4次后应用强制关闭
Manifest.xml
package="myapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />
<uses-feature android:name="android.hardware.screen.landscape" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" />
<uses-feature android:name="android.hardware.type.television" android:required="false" />
<uses-feature android:name="android.hardware.usb.host" android:required="false" />
<uses-feature android:name="android.hardware.wifi" android:required="false" />
<uses-feature android:name="android.software.leanback" android:required="false"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault"
android:banner="@drawable/splash">
<activity
android:name="myapp.ShowWebView"
android:finishOnTaskLaunch="true"
android:label="@string/chat_name"
android:theme="@android:style/Theme.DeviceDefault"
android:excludeFromRecents="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="myapp.ShowWebViewTerms"
android:finishOnTaskLaunch="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:excludeFromRecents="false" />
</application>
以及不断失败的activity:
public class ShowWebView extends Activity {
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This will not show title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_web_view);
//Get webview
webView = (WebView) findViewById(R.id.webView1);
if(haveNetworkConnection()){
startWebView("http://mywebsite/chat/index.php");
} else {
webView.loadUrl("error.html");
}
}
private void startWebView(String url) {
//Create new webview Client to show progress dialog
//When opening a url or click on link
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean Override(WebView view, String url) {
view.loadUrl(url);
return true;
}
//If url has "tel:245678" , on clicking the number it will directly call to inbuilt calling feature of phone
public boolean shouldOverrideUrlLoading(WebView view ,String url){
if(url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
} else {
view.loadUrl(url);
}
return true; }
//Show loader on url load
});
// Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
// Other webview options
/*
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setBuiltInZoomControls(true);
//Additional Webview Properties
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setLayoutAlgorithm(webView.getSettings().getLayoutAlgorithm().NORMAL);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(false);
webView.setSoundEffectsEnabled(true);
webView.setHorizontalFadingEdgeEnabled(false);
webView.setKeepScreenOn(true);
webView.setScrollbarFadingEnabled(true);
webView.setVerticalFadingEdgeEnabled(false);
*/
/*
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
*/
//Load url in webview
webView.loadUrl(url);
}
// Open previous opened link from history on webview when back button pressed
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
finish();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
不幸的是,因为我 运行 在电视盒上遇到这个错误,我无法针对实际问题进行调试。这可能是一个远景,但我是 android 开发的新手,也许我会错过一些你不会错过的东西。非常感谢任何帮助或提示。提前致谢!
启用 WiFi ADB 后,我发现清单中缺少这个
uses-permission android:name="android.permission.WAKE_LOCK"
如果有人遇到相同或类似的问题,将把它留在此处。