回调时出现空指针异常

时间:2017-05-18 14:52:50

标签: android nullpointerexception callback

我从here

获得了一些活动识别的例子

问题是我希望用结果更新我的GUI,所以我尝试了回调但是我得到了null异常。你能帮助我或给别的选择吗?

主类:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, MyCallback  {

public GoogleApiClient mApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Intent intent = new Intent(this, ActivityRecognizedService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, 3000, pendingIntent);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}


@Override
public void callbackCall(int i)
{
    //Here will be gui Update
    Log.d("DTAG", "is: "+i);
}
}

回调界面:

interface MyCallback {
void callbackCall(int i);
}

ActivityRecognizedService:

public class ActivityRecognizedService extends IntentService {

    MyCallback callback;

    public ActivityRecognizedService() {
        super("ActivityRecognizedService");
    }

    public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities( result.getProbableActivities() );
        }
    }

    void onEvent() {

    }

    private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
        for( DetectedActivity activity : probableActivities ) {
            switch( activity.getType() ) {
                case DetectedActivity.IN_VEHICLE: {
                    Log.e( "ActivityRecogition", "In Vehicle: " + activity.getConfidence() );
                    callback.callbackCall(activity.getConfidence());
                    //return value for GUI update
                    break;
                }

                case DetectedActivity.UNKNOWN: {
                    Log.e( "ActivityRecogition", "Unknown: " + activity.getConfidence() );
                    break;
                }
            }
        }
    }
}

例外:

java.lang.NullPointerException: Attempt to invoke interface method 'void dan.com.movetest.MyCallback.callbackCall(int)' on a null object reference
                                                                           at dan.com.movetest.ActivityRecognizedService.handleDetectedActivities(ActivityRecognizedService.java:49)
                                                                           at dan.com.movetest.ActivityRecognizedService.onHandleIntent(ActivityRecognizedService.java:36)

3 个答案:

答案 0 :(得分:1)

您尚未在callback中的任何位置初始化ActivityRecognizedService。在这种情况下,回调可能不是最佳方法,引用答案from another question

  

您可能正在寻找ResultReceiver类(http://developer.android.com/reference/android/os/ResultReceiver.html)。   你扩展它,将它与你调用服务的意图一起传递(它是可以分配的),然后当你的任务在服务中完成时,你用结果调用ResultReceiver的onReceiveResult()方法。

答案 1 :(得分:0)

使用((MyCallback)activity) .callbackCall(activity.getConfidence());

case DetectedActivity.IN_VEHICLE:{

答案 2 :(得分:0)

您需要启动回调才能使其正常工作。

尝试在ActivityRecognizedService构造函数中传递MyCallback实例,并在MainActivity上启动ActivityRecognizedService时执行以下操作:

new ActivityRecognizedService(new MyCallback(...))

并实现回调的方法。 我希望这有帮助