5

我的 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)
4

2 回答 2

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);

        }
于 2012-07-24T12:03:59.200 回答
0

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

于 2013-05-25T13:21:26.133 回答