Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Service Messenger响应空对象引用?_Java_Android_Service_Response_Messenger - Fatal编程技术网

Java Android Service Messenger响应空对象引用?

Java Android Service Messenger响应空对象引用?,java,android,service,response,messenger,Java,Android,Service,Response,Messenger,我通过在我的服务IncomingHandler中调用msg.replyTo(rsp)得到空对象引用错误 public class MessengerService extends Service { /** Command to the service to display a message */ static final int MSG_SAY_HELLO = 1; /** * Handler of incoming messages from clients. */ class In

我通过在我的服务IncomingHandler中调用msg.replyTo(rsp)得到空对象引用错误

public class MessengerService extends Service {
/** Command to the service to display a message */
static final int MSG_SAY_HELLO = 1;

/**
 * Handler of incoming messages from clients.
 */
class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_SAY_HELLO:
                Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show();
                Message resp = Message.obtain(null,msg.what);
                Bundle bResp = new Bundle();
                bResp.putString("respData", "Uppercase");
                resp.setData(bResp);
                try {
                   msg.replyTo.send(resp);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            default:
                super.handleMessage(msg);
        }
    }
}

/**
 * Target we publish for clients to send messages to IncomingHandler.
 */
final Messenger mMessenger = new Messenger(new IncomingHandler());

/**
 * When binding to the service, we return an interface to our messenger
 * for sending messages to the service.
 */
@Override
public IBinder onBind(Intent intent) {
    Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
    return mMessenger.getBinder();
   }
}
服务正在运行,我可以向我的服务发送消息

我的主要活动如下(应该可以正常工作):

}

这是我的舱单(我想这个舱单已经很好了):



目标是从我的服务向我的MainActivity发送响应。

调用
onServiceConnected
中的
sayHello
方法,并将null传递给参数

调用
onServiceConnected
中的
sayHello
方法,并将null传递给参数

将“messenger”设为类变量,之后为:Message msg=Message.get(null,MessengerService.msg\u SAY\u HELLO,0,0);put:msg.replyTo=信使非常感谢。。它起作用了:)使“messenger”成为一个类变量,之后是:Message msg=Message.get(null,messenger service.msg_SAY_HELLO,0,0);put:msg.replyTo=信使非常感谢。。成功了:)
public class MainActivity extends Activity {
/** Messenger for communicating with the service. */
Messenger mService = null;

/** Flag indicating whether we have called bind on the service. */
boolean mBound;

/**
 * Class for interacting with the main interface of the service.
 */
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with the service has been
        // established, giving us the object we can use to
        // interact with the service.  We are communicating with the
        // service using a Messenger, so here we get a client-side
        // representation of that from the raw IBinder object.
        mService = new Messenger(service);
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName className) {
        // This is called when the connection with the service has been
        // unexpectedly disconnected -- that is, its process crashed.
        mService = null;
        mBound = false;
    }
};

public void sayHello(View v) {
    if (!mBound) return;
    // Create and send a message to the service, using a supported 'what' value
    Message msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
    try {
        mService.send(msg);
        Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();

    } catch (RemoteException e) {
        e.printStackTrace();
    }
}
private class ResponseHandler extends Handler {
    @Override
    public void handleMessage(Message message) {
        Toast.makeText(getApplicationContext(), "message from service",Toast.LENGTH_SHORT).show();
    }
}

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

    Messenger messenger = new Messenger(new ResponseHandler());

}

@Override
protected void onStart() {
    super.onStart();
    // Bind to the service
    bindService(new Intent(this, MessengerService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
 }
<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="app.test.mservs.fronservicesetc">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".MessengerService" >
    </service>
</application>