2013-05-16 66 views
0

我建立我的第一个应用程序,我希望当按下按钮,手机不动,让我的手机的方向。用户可以看到后视摄像头的观点,然后按下按钮即可获取角度。调用getOrientation()导致崩溃

当我在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。

+2

发布logcat – Blackbelt

+0

是的,因为你是android dev的新手,logcat会向你显示崩溃和错误报告的堆栈跟踪。 Eclipse应该有它或做“adb logcat”。 – spartygw

+0

我设法发现那部分崩溃是: 'switch(event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: gravity = event.values.clone(); 休息; case Sensor.TYPE_MAGNETIC_FIELD: magnet = event.values.clone(); 休息; }” 也许任何想法,为什么或如何让不同的方式的价值观? –

回答

0

您的event字段永远不会初始化,您必须获得NullPointerException,同时尝试在您的switch语句中访问它。

您应该执行SensorEventListener并在onSensorChanged()中获得事件,否则您将永远不会拥有这些值。

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

+0

我添加了整个班级。 –

+0

仍然没有logcat ...(我们需要知道确切的异常,这里我认为它; ight是一个'NullPointerException',对吧?)但我会用我现在得到的东西编辑我的答案。 – Joffrey

+0

我再次编辑,使其更易于阅读,因为它已被接受,很高兴我帮助。 – Joffrey

0

我增加了onSensorChange并实时计算值,所以现在一切都准确无误。感谢您的帮助。