android远程服务空指针

时间:2015-08-06 18:04:12

标签: android service nullpointerexception

我不再是Java / Android的初学者,但这个问题让我陷入困境。我有一个调用远程服务的主项目。在运行主项目之前,在手机上安装了远程服务(ROS)。但是,当我调用rosService.publishCommand(评论的行' Release Emergency Brake')时,我在serviceManager中不断收到空指针异常。我不认为这是竞争条件,因为日志文件在调用publishCommand之前显示成功的连接。此外,您可以看到日志显示onBind()已在远程服务中输入。如果您有任何想法,请告诉我! 谢谢!

package org.ros.android.jaguar;

import org.ros.android.jaguar.ROSService;
import org.ros.android.jaguar.ROSServiceReporter;

import com.darpa.uvade.Settings;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;


public class ROSServiceManager {

    private boolean disconnected = false;
    private ROSService rosService;
    private static Intent intent = new Intent("com.darpa.ros");
    private OnConnectedListener onConnectedListener;
    private Service service;
    private Activity activity;
    private String tag = "ROS";

    public static interface OnConnectedListener {
        void onConnected();
        void onDisconnected();
    }

    public ROSServiceManager(Service service, OnConnectedListener onConnectedListener) {
        this.service = service;
        this.onConnectedListener = onConnectedListener;
        service.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }
    /*public ROSServiceManager(Activity activity, OnConnectedListener onConnectedListener) {
        this.activity = activity;
        this.onConnectedListener = onConnectedListener;
        activity.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }*/


    public void publishCommand(float velocity, float angle){

        String initString = "MMW !M ";

        if(velocity>1){
            Log.e("UVADE","Error! Velocity too large");
            velocity=1;
        }

        //Log.d(tag, "velocity: "+velocity+", angle: "+angle+", maxPower: "+Settings.getJaguarMaxPower());

        int jaguarX = (int) ((int) Settings.getJaguarMaxPower() * Math.cos(angle - 45*Math.PI/180) * velocity);
        int jaguarY = (int) ((int) Settings.getJaguarMaxPower() * Math.sin(angle - 45*Math.PI/180) * velocity);


        String commandString = initString.concat(String.valueOf(jaguarX)+" "+jaguarY);
        Log.d(tag,commandString);

        SystemClock.sleep(1000);

        Log.d(tag, "rosService right before null Pointer: "+rosService);

        try {
            rosService.publishCommand(initString+"G");  //release emergency brake
            rosService.publishCommand(commandString);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NullPointerException e1){
            e1.printStackTrace();
        }
    }

    public synchronized void disconnect() {
        disconnected = true;
        if(service!=null)
            service.unbindService(serviceConnection);
        else
            activity.unbindService(serviceConnection);
    }
    public synchronized void add(ROSServiceReporter reporter) {
        if (disconnected)
            throw new IllegalStateException("Manager has been explicitly disconnected; you cannot call methods on it");
        try {
            rosService.add(reporter);
        } catch (RemoteException e) {
            Log.e(tag, "add reporter", e);
        }
    }
    public synchronized void remove(ROSServiceReporter reporter) {
        if (disconnected)
            throw new IllegalStateException("Manager has been explicitly disconnected; you cannot call methods on it");
        try {
            rosService.remove(reporter);
        } catch (RemoteException e) {
            Log.e(tag, "remove reporter", e);
        }
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override public void onServiceDisconnected(ComponentName name) {
            Log.d(tag, "RosService onServiceDisconnected");
            if (onConnectedListener != null)
                onConnectedListener.onDisconnected();
            rosService = null;
        }
        @Override public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(tag,"RosService onServiceConnected");
            rosService = ROSService.Stub.asInterface(service);
            Log.d(tag, "rosService: "+rosService);

            if (onConnectedListener != null)
                onConnectedListener.onConnected();
        }
    };


}

运行时,这是日志输出:

08-06 13:48:43.721: D/ROS(7540): ROS onCreate

08-06 13:48:43.731: D/ROS(7540): ROS Service onBind

08-06 13:48:43.731: D/ROS(7540): Returning Binder

08-06 13:48:43.731: D/ROS(7483): RosService onServiceConnected

08-06 13:48:43.731: D/ROS(7540): RosService onServiceConnected

08-06 13:48:43.731: D/ROS(7540): Jaguar connected - adding reporter

08-06 13:48:43.741: D/ROS(7483): rosService: org.ros.android.jaguar.ROSService$Stub$Proxy@431529e0

08-06 13:48:46.613: D/ROS(7483): MMW !M 198 -671

08-06 13:48:47.614: D/ROS(7483): rosService right before null Pointer: null

08-06 13:48:47.715: D/ROS(7483): MMW !M 198 -671

08-06 13:48:48.715: D/ROS(7483): rosService right before null Pointer: null

08-06 13:48:48.766: D/ROS(7483): MMW !M 198 -671

0 个答案:

没有答案
相关问题