调用onConnectionFailed时出现NullPointerException

时间:2016-07-14 16:52:28

标签: java android nullpointerexception

我在尝试拨打NullPointerException时遇到onConnectionFailed时遇到问题。

我已遵循此特定GoogleApi功能的指南,并更新了其折旧电话。

但是,当我尝试调用它时,它会给我一个NullPointerException,即使我认为我覆盖了所有指针并正确初始化它们。

这是我的onConnectionFailed实施:

public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            FailedConnection();
        }
    } else {
        GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
        int StatusCode = apiAvailability.isGooglePlayServicesAvailable(this);
        apiAvailability.getErrorDialog(this, StatusCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
    }
}

我最初认为这是全局变量和局部变量的问题,虽然我已经将指针调用复制到本地范围,它仍然给我一个NullPointerException

这是logcat,如果有人需要它:

y.phonebelt E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=0, data=null} to activity {com.example.denny.phonebelt/com.example.denny.phonebelt.ViewingWindow}: java.lang.NullPointerException
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
     at android.app.ActivityThread.access$1200(ActivityThread.java:159)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:137)
     at android.app.ActivityThread.main(ActivityThread.java:5419)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:525)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
     at com.example.denny.phonebelt.ViewingWindow.onConnectionFailed(ViewingWindow.java:214)
     at com.google.android.gms.internal.zzpp.zza(Unknown Source)
     at com.google.android.gms.internal.zzps.onActivityResult(Unknown Source)
     at com.google.android.gms.internal.zzra.onActivityResult(Unknown Source)
     at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:165)
     at android.app.Activity.dispatchActivityResult(Activity.java:5563)
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543) 
     at android.app.ActivityThread.access$1200(ActivityThread.java:159) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5419) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
     at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

你可以试试下面的代码吗?

根据DOCS,如果<?php $regex_array = array( "/regex_1/" => 0, "/regex_2/" => 0, "/regex_3/" => 0 // and so on and so forth ); $strings_array = array( "input_string_1", "input_string_2", "input_string_3" // and so on and so forth ); foreach ($regex_array as $key => $value) foreach ($strings_array as $current_string) if (preg_match($key, $current_string)) $regex_array[$key]++; ?>

,方法getErrorDialog()可以返回null
errorCode == SUCCESS

另一个选项是public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { if (connectionResult.hasResolution()) { try { connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE); } catch (IntentSender.SendIntentException e) { FailedConnection(); } } else { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int StatusCode = apiAvailability.isGooglePlayServicesAvailable(this); if(StatusCode != ConnectionResult.SUCCESS) apiAvailability.getErrorDialog(this, StatusCode, PLAY_SERVICES_RESOLUTION_REQUEST).show(); } } 但是无法检查你如何定义它。