Service中的onBind()提供Messenger对象的空对象引用

时间:2018-09-08 12:25:14

标签: android service

我试图将服务绑定到我的主要活动,但是从Service调用onBind()方法时遇到java.lang.NullPointerException错误。

此方法应从通常已初始化的Messenger对象返回IBinder对象类型。

我可以看到初始化我的Messenger的方法(onImageDownloaderThreadPrepared)在错误发生之前就被调用了,所以我不知道为什么会遇到这种行为。

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

private boolean mBound;
private Messenger mRemoteService = null;

private ServiceConnection mRemoteConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mRemoteService = new Messenger(service);
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mRemoteService = null;
        mBound = false;
    }
};

@SuppressLint("HandlerLeak")
public void sendOnClick(View v) {
    if (mBound) {
        Log.i("TAG", "Service is bounded");
        try {
            Message msg = Message.obtain(null, 1, 0, 0);
            msg.replyTo = new Messenger(new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    Log.i("TAG", "Message sent back - msg.what :" + msg.what);
                }
            });
            mRemoteService.send(msg);
        } catch (RemoteException e) {
            Log.i("TAG", e.getMessage());
        }
    } else Log.i("TAG", "Service is unbounded");
}

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

    Intent intent = new Intent(
            "com.example.android.td5servicesexo3.ImageDownloaderThreadService.ACTION_BIND");
    intent.setPackage("com.example.android.td5servicesexo3");
    bindService(intent, mRemoteConnection, Context.BIND_AUTO_CREATE);
}}

这是我的服务班

public class ImageDownloaderThreadService extends Service {

ImageDownloaderThreadService.ImageDownloaderThread mImageDownloaderThread;
Messenger mImageDownloaderMessenger;

@Override
public void onCreate() {
    super.onCreate();
    mImageDownloaderThread = new ImageDownloaderThread();
    mImageDownloaderThread.start();
}

// When this is called, looper and handler's thread are prepared
private void onImageDownloaderThreadPrepared() {
    mImageDownloaderMessenger =
            new Messenger(mImageDownloaderThread.mImageDownloaderHandler);
}

@Override
public IBinder onBind(Intent intent) {
    return mImageDownloaderMessenger.getBinder();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mImageDownloaderThread.quit();
}

/** ImageDownloaderThread */
private class ImageDownloaderThread extends Thread {

    Handler mImageDownloaderHandler;

    @Override
    @SuppressLint("HandlerLeak")
    public void run() {
        Looper.prepare();
        mImageDownloaderHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        try {
                            msg.replyTo.send(Message.obtain(
                                    null, msg.what, 0, 0));
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
        onImageDownloaderThreadPrepared();
        Looper.loop();
    }

    void quit() {
        mImageDownloaderHandler.getLooper().quit();
    }
}}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

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

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

    <service
        android:enabled="true"
        android:name="com.example.android.td5servicesexo3.ImageDownloaderThreadService" >
        <intent-filter>
            <action android:name="com.example.android.td5servicesexo3.ImageDownloaderThreadService.ACTION_BIND" />
        </intent-filter>
    </service>
</application>

错误

09-08 14:19:04.981 18933-18933/com.example.android.td5servicesexo3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.td5servicesexo3, PID: 18933
java.lang.RuntimeException: Unable to bind to service com.example.android.td5servicesexo3.ImageDownloaderThreadService@78b2537 with Intent { act=com.example.android.td5servicesexo3.ImageDownloaderThreadService.ACTION_BIND pkg=com.example.android.td5servicesexo3 }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.os.Messenger.getBinder()' on a null object reference
    at android.app.ActivityThread.handleBindService(ActivityThread.java:3378)
    at android.app.ActivityThread.-wrap3(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1664)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:6523)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.os.Messenger.getBinder()' on a null object reference
    at com.example.android.td5servicesexo3.ImageDownloaderThreadService.onBind(ImageDownloaderThreadService.java:33)
    at android.app.ActivityThread.handleBindService(ActivityThread.java:3362)
    at android.app.ActivityThread.-wrap3(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1664) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:156) 
    at android.app.ActivityThread.main(ActivityThread.java:6523) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 

0 个答案:

没有答案