Android WebView活动开始然后在5.1.1崩溃

时间:2017-07-09 22:39:51

标签: android android-activity webview crash android-5.1.1-lollipop

好的,对于所有人来说,这是一个非常奇怪的事情。我有一个非常简单的Android应用程序,包含两个webview活动。他们(显然)都在查看我服务器上托管的html网页。这就是问题所在;该应用程序在我的6.0.1设备上运行得非常好,但是当我在我的5.1.1电视盒上运行时,它会像平常一样启动,文本输入字段显示闪烁的指示器,所以我知道它正常运行。指示灯将闪烁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>

持续失败的活动:

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开发的新手,也许我想念你赢得的东西。非常感谢任何帮助或提示。提前谢谢!

1 个答案:

答案 0 :(得分:1)

启用WiFi ADB后,我能够发现清单缺少此

 uses-permission android:name="android.permission.WAKE_LOCK"    

如果有人遇到相同或类似的问题,请留下此处。