android webview函数onPagefinished被调用两次

时间:2012-07-22 14:32:20

标签: android android-webview

我的android webview函数onPageFinished被调用两次。我不知道为什么,但它在Android 2.2上运行良好但是当我将它升级到4+时它不起作用。代码附在下面

代码

@Override  
            public void onPageFinished(WebView view, String url)  {  


                if (url.startsWith(MobileConstants.PUSHPIN_CALLBACK_URL)) {
                    if (url.indexOf("code=")!=-1) {

                        String code = url.substring(url.indexOf("code=")+5);
                        Log.i ("code", code);
                        if (code != null && !code.equals("")  ){
                            view.setVisibility(View.GONE);
                            Token accessToken = service.getAccessToken(null, new Verifier(code));
                            Log.i("access_token",accessToken.getToken());


                            SharedPreferences settings = getSharedPreferences("access_token" , 0);
                            SharedPreferences.Editor editor = settings.edit();
                            if(accessToken.getToken() != null)
                                editor.putString("access_token", accessToken.getToken() );


                            Intent map3 = new Intent(OAuthAccessTokenActivity.this, Dashboard.class);

                            map3.putExtra("access_token", accessToken.getToken());
                            startActivity(map3);
                        }


                    } else if (url.indexOf("error=")!=-1) {
                        view.setVisibility(View.INVISIBLE);

                    }

                }
                System.out.println("onPageFinished : " + url);

            }

日志

07-22 14:29:50.523: E/AndroidRuntime(1186): FATAL EXCEPTION: main
07-22 14:29:50.523: E/AndroidRuntime(1186): java.lang.NullPointerException
07-22 14:29:50.523: E/AndroidRuntime(1186):     at com.facebook.android.OAuthAccessTokenActivity$1.onPageFinished(OAuthAccessTokenActivity.java:83)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:327)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at android.os.Looper.loop(Looper.java:137)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invoke(Method.java:511)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-22 14:29:50.523: E/AndroidRuntime(1186):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

我通过将代码从onPageFinished移动到此函数

来解决了这个问题

<强>代码

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){

            if (url.startsWith(MobileConstants.CALLBACK_URL)) {
                if (url.indexOf("code=") !=  -1) {

                    String code = url.substring(url.indexOf("code=")+5);
                    Log.i ("code", code);
                    if (code != null && !code.equals("")  ){
                        view.setVisibility(View.GONE);
                        Token accessToken = service.getAccessToken(null, new Verifier(code));
                        Log.i("access_token",accessToken.getToken());


                        SharedPreferences settings = getSharedPreferences("access_token" , 0);
                        SharedPreferences.Editor editor = settings.edit();
                        if(accessToken.getToken() != null)
                            editor.putString("access_token", accessToken.getToken() );

                        access = accessToken.getToken();

                        Intent map3 = new Intent(OAuthAccessTokenActivity.this, Dashboard.class);

                        map3.putExtra("access_token", accessToken.getToken());
                        startActivity(map3);
                        return true;
                    }


                } else if (url.indexOf("error=")!=-1) {
                    view.setVisibility(View.INVISIBLE);
                    return false;
                }

            }
            System.out.println("onPageFinished : " + url);

            return super.shouldOverrideUrlLoading(view, url);

        }

答案 1 :(得分:0)

我遇到了同样的问题,但我只需要停止进度条。 我“解决”它再次启动方法onPageStarted中的进度条。这不是一个好的解决方案,但在我的情况下已经足够了。