1

我已经增强了这个相机代码,我可以毫无问题地拍照:http: //mobile.tutsplus.com/tutorials/android/android-essentials-create-a-mirror/

我创建了一个有界服务,它通过 Internet 接收命令并启动一个可运行线程,然后它会拍照。我对有界服务和启动可运行线程的问题为零。当我的手机向服务器发送命令并通过服务接收回命令时,我可以毫无问题地拍照。但是,如果外部设备发送命令,可运行线程会运行,但随后会在 mCamera.takePicture() 处崩溃,并出现空指针异常。

public class RunnableEventA implements Runnable {

    private Camera mCamera;
    private MirrorActivity mActivity;



    public RunnableEventA(Camera camera, Activity activity) {
        mCamera = camera;
        mActivity = (MirrorActivity) activity;
    } 

    public void run() {
        Log.d("sent", "Runnable here");
        //mCamera.takePicture(null, null, this);
        mCamera.takePicture(null, null, mActivity);
    }

}

Activity和mCamera通过onConnected方法中MirrorActivity传入的有界服务传递给可运行线程:

/** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();

            mBound = true;

            /* Send parameters to Service*/
            mService.setParams(MirrorActivity.this, mCamera);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

关于我做错了什么或需要添加的任何想法?我不想用代码来夸大这篇文章,所以如果你认为你需要看到任何额外的东西,请告诉我。

4

0 回答 0