2012-05-18 59 views
0

我無法找到這裏代碼中的錯誤。我試圖獲取acceletometer數據,但是當我嘗試在設備上運行它時,它出現了一條消息,該進程已被關閉。我的加速度計代碼有問題

public class SensorActivity extends Activity implements SensorEventListener { 
SensorManager sm; 
Sensor sensor ; 
TextView yViewA = null; 
TextView zViewA = null; 
TextView xViewO = null; 
TextView yViewO = null; 
TextView zViewO = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


sm = (SensorManager)getSystemService(SENSOR_SERVICE); 
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
xViewA = (TextView) findViewById(R.id.xbox); 
yViewA = (TextView) findViewById(R.id.ybox); 
zViewA = (TextView) findViewById(R.id.zbox); 
xViewO = (TextView) findViewById(R.id.xboxo); 
yViewO = (TextView) findViewById(R.id.yboxo); 
zViewO = (TextView) findViewById(R.id.zboxo); 


}  

@Override 
protected void onResume() { 
    super.onResume(); 
    sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); 
} 

@Override 
protected void onStop() { 
    sm.unregisterListener(this); 
    super.onStop(); 
} 

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

} 

public void onSensorChanged(SensorEvent event) { 
    // TODO Auto-generated method stub 

    xViewO.setText("Orientation X: " + event.values[0]); 
    yViewO.setText("Orientation Y: " + event.values[1]); 
    zViewO.setText("Orientation Z: " + event.values[2]); 

} 

UPD:

05-18 19:42:17.570: E/AndroidRuntime(14875): android.app.SuperNotCalledException: Activity {daler.sensor/daler.sensor.SensorActivity} did not call through to super.onCreate() 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1933) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.access$600(ActivityThread.java:127) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.os.Handler.dispatchMessage(Handler.java:99) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.os.Looper.loop(Looper.java:137) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at android.app.ActivityThread.main(ActivityThread.java:4441) 

05-18 19:42:17.570: E/AndroidRuntime(14875): at java.lang.reflect.Method.invokeNative(Native Method) 

UPD2:代碼被更新,仍然面臨的問題。 UPD3:日誌貓更新

UPD4:我更新了代碼,現在它工作。也許有人會需要它。謝謝。

+1

粘貼logcat的太 – waqaslam

回答

3

你不能把findViewById(R.id.xbox);放在你的課堂上。它應該在setContentView()之後去onCreate()

問題出現在代碼的第18行(根據logcat)。這也可能是這一個:

Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

因爲你寫的: SensorManager sm; 因此,傳感器管理爲空,所以你不能在sm.getDefaultSensor()使用它。

看到這個link找出如何initalize傳感器經理:

public SensorActivity() { 
    sm = (SensorManager)getSystemService(SENSOR_SERVICE); 
    sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
} 
+0

非常感謝你對你的回答很即時欣賞它。不幸的是它仍然無法正常工作。我更新logcat。 – Daler

+0

您仍然沒有初始化SensorManager。看到更新的答案 –

+0

它給了我錯誤,當我把sm =新的SensonManager();在onCreate下,當我移動texviews時,logcat中沒有更多的21行。 – Daler

1

在您的onCreate()嘗試從所有6條findViewById語句去掉開頭的TextView。

變化從

TextView xViewA = (TextView) findViewById(R.id.xbox); 

xViewA = (TextView) findViewById(R.id.xbox); 

編輯:

public class SensorActivity extends Activity implements SensorEventListener { 

SensorManager sm; 
Sensor sensor; 
TextView xViewA = null; 
TextView yViewA = null; 
TextView zViewA = null; 
TextView xViewO = null; 
TextView yViewO = null; 
TextView zViewO = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

sm = (SensorManager)getSystemService(SENSOR_SERVICE); 
sensor= sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); 

xViewA = (TextView) findViewById(R.id.xbox); 
yViewA = (TextView) findViewById(R.id.ybox); 
zViewA = (TextView) findViewById(R.id.zbox); 
xViewO = (TextView) findViewById(R.id.xboxo); 
yViewO = (TextView) findViewById(R.id.yboxo); 
zViewO = (TextView) findViewById(R.id.zboxo); 

setContentView(R.layout.main); 

} 
.. 
+0

仍然,沒有工作。我更新了代碼 – Daler

+0

現在上傳最新的logcat。 – TheCottonSilk

+0

已更新。請 – Daler