【问题标题】:Calling getOrientation() causes crash调用 getOrientation() 会导致崩溃
【发布时间】:2013-05-16 15:36:41
【问题描述】:

我正在构建我的第一个应用程序,我想在按下按钮但手机不动时获取手机的方向。用户可以看到后置摄像头的视图,指向然后按下按钮,应该可以得到角度。

当我在onSensorChanged 中使用它时,该代码似乎可以正常工作(我使用的是来自:finding orientation using getRotationMatrix() and getOrientation() 的示例代码),但是当我在“onClick”中使用它时会崩溃。

我想获取其他计算的音高值(我制作了音高全局变量)。 这是我的onClick 代码:

public void onClick(View arg0)
{
    camera.takePicture(myShutterCallback,
            myPictureCallback_RAW, myPictureCallback_JPG);

    switch (event.sensor.getType()) {
    case Sensor.TYPE_ACCELEROMETER:
        gravity = event.values.clone();
        break;
    case Sensor.TYPE_MAGNETIC_FIELD:
        magnet = event.values.clone();
        break;
    }


    if(gravity!=null && magnet!=null) {
        rotMatrix = SensorManager.getRotationMatrix(inR, I, gravity, magnet);

        SensorManager.remapCoordinateSystem(inR,
                SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
        SensorManager.getOrientation(outR, orientVals);

        float azimuth = orientVals[0]*rad2deg;
        pitch = orientVals[1]*rad2deg;
        float roll = orientVals[2]*rad2deg;

        TextView rezDisplay = (TextView) findViewById(R.id.finalRezult);
        rezDisplay.setText("Atstumas iki objekto yra:" + pitch + "m");
    }
}

任何建议、链接、示例或 tuts 都会很棒。

编辑:

我添加了整个类的代码。

import java.io.IOException;

import com.mindjack.measuremydistance.SensorEvnt.mSensorEventListener;

import android.app.Activity;
//import android.content.Intent;
import android.content.Context;
import android.content.pm.ActivityInfo;
//import android.graphics.Bitmap;
//import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class AndroidCamera extends Activity implements SurfaceHolder.Callback
{
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;
    LayoutInflater controlInflater = null;
    TextView display, display1;
    float[] magnet = new float[3];
    float[] gravity = new float[3];
    SensorEvent event;
    boolean rotMatrix;
    float[] outR = new float[16];
    float[] inR = new float[16];
    float[] I = new float[16];
    float[] orientVals = new float[3];
    final float pi = (float) Math.PI;
    final float rad2deg = 180/pi;
    float pitch;
    SensorManager mSensorManager;


    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.andcam);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl
            = new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(new Button.OnClickListener() {
            // TODO Auto-generated method stub
            @Override
            public void onClick(View arg0) {
                camera.takePicture(myShutterCallback,
                     myPictureCallback_RAW, myPictureCallback_JPG);

                switch (event.sensor.getType()){
                case Sensor.TYPE_ACCELEROMETER:
                    gravity = event.values.clone();
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    magnet = event.values.clone();
                    break;
                }

                //if(gravity!=null && magnet!=null){
                    rotMatrix = SensorManager.getRotationMatrix(inR,
                        I, gravity, magnet);
                    SensorManager.remapCoordinateSystem(inR,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
                    SensorManager.getOrientation(outR, orientVals);

                    float azimuth = orientVals[0]*rad2deg;
                    pitch = orientVals[1]*rad2deg;
                    float roll = orientVals[2]*rad2deg;
                // }

                TextView rezDisplay = (TextView) findViewById(R.id.finalRezult);
                rezDisplay.setText("Atstumas iki objekto yra:" + pitch + "m");
            }
        });
    }

    ShutterCallback myShutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
            // TODO Auto-generated method stub
        }
    };

    PictureCallback myPictureCallback_RAW = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub
        }
    };

    PictureCallback myPictureCallback_JPG = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub
            //Bitmap bitmapPicture
            //= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height)
    {
        // TODO Auto-generated method stub
        if(previewing) {
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera = Camera.open();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}

由于我是新手,可能会有一些愚蠢的错误,但我无法弄清楚这种情况。 LogCat 说它在第 84 行崩溃了。我试图从第 84 行到第 91 行取出部分,然后它没有崩溃,但在音高值上也给出了 0。

【问题讨论】:

  • 是的,由于您是 android dev 的新手,logcat 会向您显示崩溃和错误报告的堆栈跟踪。 Eclipse 应该拥有它或执行“adb logcat”。
  • 我设法发现崩溃的部分是:'switch (event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER:gravity = event.values.clone();休息; case Sensor.TYPE_MAGNETIC_FIELD: 磁铁 = event.values.clone();休息; }' 也许任何想法为什么或如何以不同的方式获取值?

标签: android android-sensors android-orientation


【解决方案1】:

您的event 字段从未初始化,您必须在您的switch 语句中尝试访问它时获得NullPointerException

您应该实现SensorEventListener 并在onSensorChanged() 中获取事件,否则您将永远无法获得值。

但是,不要直接存储SensorEvent,因为该对象会被SensorManager 重用。一旦您在onSensorChanged() 中获取传感器的值,就必须将它们复制到其他字段(如 switch 语句中的变量)。然后您可以在onClick() 中使用这些字段。

【讨论】:

  • 仍然没有 logcat...(我们需要知道确切的例外情况,在这里我认为它;应该是 NullPointerException,对吗?)但我会用我的答案编辑我的答案现在得到了。
  • 我再次编辑以使帖子更具可读性,因为它已被接受,很高兴我提供了帮助。
【解决方案2】:

我添加了 onSensorChange 并实时计算值,所以现在一切正常。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多