java.lang.ArrayIndexOutOfBoundsException:在android中使用webview方法shouldOverrideUrlLoading

时间:2017-06-01 04:12:03

标签: android android-studio android-sdk-2.3

java.lang.ArrayIndexOutOfBoundsException:  在com.example.fragments.Hot $ 4.shouldOverrideUrlLoading(Hot.java:197)在com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:357)在org.chromium.android_webview.AwContentsClient.shouldIgnoreNavigation(AwContentsClient。的java:168)在org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:352)在org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(母语方法:0)在org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler。 java:41)在android.os.Handler.dispatchMessage(Handler.java:102)的android.app.Looper.loop(Looper.java:148)android.app.ActivityThread.main(ActivityThread.java:7406)at java.lang.reflect.Method.invoke(Native Method:0)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit)的.java:1120)

这是我的代码:

  hot_webview.setWebViewClient(new WebViewClient()
    {

        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {

            String checkurl = url;
            String[] separated = checkurl.split("=");
            String splitted=separated[1];


            Log.i("iaminh", " shouldOverrideUrlLoading called");

            boolean installed = appInstalledOrNot(splitted);
            if(installed)
            {

                Intent LaunchIntent = getActivity().getPackageManager()
                        .getLaunchIntentForPackage(splitted);
                startActivity(LaunchIntent);


                hot_webview.loadUrl(Hot_url);

            } else
            {


                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                            .parse(market_url + splitted)));

                    hot_webview.loadUrl(Hot_url);

                } catch (ActivityNotFoundException anfe)
                {
                    startActivity(new Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse(playstore_url + splitted)));

                    hot_webview.loadUrl(Hot_url);
                }

            }






            return false;
        }

    });

1 个答案:

答案 0 :(得分:0)

问题在于这段代码

String splitted=separated[1];

每当你处理数组时,在访问数组中的元素之前进行检查是安全的。像

这样的东西
if(separated.length>2){
// do your stuff here
 String splitted=separated[1];
}else{
//handle failure case here
}