2015-12-20 32 views
0

我通過在我的Service-IncomingHandler中調用msg.replyTo(rsp)來獲得空對象引用錯誤。Android Service Messenger響應空對象引用?

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是如下(應該是工作的罰款):

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> 

目標是將我的服務的響應發送到我的MainActivity。

+0

使'messenger'成爲一個類變量,之後:Message msg = Message.obtain(null,MessengerService.MSG_SAY_HELLO,0,0);把:msg.replyTo =信使 –

+0

非常感謝你..它工作:) – apolol92

回答

0

請致電sayHello方法onServiceConnected,並將null傳遞給參數。