2012-03-21 91 views
0

我與加速度計爲Android的工作,我不明白爲什麼三軸他們我所分配到mSensorX,mSensorY和mSensorZ被標記爲未使用的,當我在onSensorChange使用它們,可能有人請幫助我明白這一點。加速度計代碼

package com.example.imageaccel; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.widget.TextView; 


public class ImageAccelActivity extends Activity implements SensorEventListener { 
    /** Called when the activity is first created. */ 
    TextView x, y, z; 
    private float mSensorX; 
    private float mSensorY; 
    private float mSensorZ; 
    private Bitmap car; 


    private SensorManager sm = null; 
// Bitmap car1; 
// float x1, y1, z1, sensorX, sensorY, sensorZ; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    x = (TextView)findViewById(R.id.x_axis); 
    y = (TextView)findViewById(R.id.y_axis); 
    z = (TextView)findViewById(R.id.z_axis); 

    SensorManager sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
    if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() !=0){ 
     Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); 
     sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL); 

    } 

} 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

     sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     sm.unregisterListener(this); 
     super.onPause(); 
    } 
    @Override 

    public void onAccuracyChanged(Sensor arg0, int arg1) { 
     // TODO Auto-generated method stub 


    } 


    @Override 
    public void onSensorChanged(SensorEvent ev) { 
     // TODO Auto-generated method stub 

     if(ev.sensor.getType()==Sensor.TYPE_ACCELEROMETER){ 

      mSensorX = ev.values[0]; 
      mSensorY = ev.values[1]; 
      mSensorZ = ev.values[2]; 


     } 

} 
     protected void onDraw(Canvas canvas) { 

       /* 
       * draw the background 
       */ 

       canvas.drawBitmap(car, 0, 0, null); 

    } 


     } 

回答

1

設置它們的值,但是,這只是故事的一半。投訴是最有可能的是你永遠使用他們之後。它類似於C代碼:

int main (void) { 
    int a = 1; 
    return 0; 
} 

雖然是編譯和運行良好,你就得到一個警告(使用gcc -Wall)認爲:

warning: unused variable 'a' 

檢查這一理論快捷的方式,把一個:

System.out.println (mSensorX + "," + mSensorY + "," + mSensorZ); 

(或某種其他類型的用法),看看是否警告消失。

0

我認爲有一個警告,因爲if語句在那裏..在某些情況下,他們不會被使用,因此警告..這是我的意見:)