2

我现在正在学习 android 的加速度计,我找到了一些代码并尝试制作加速度计,但是我的应用程序突然停止并显示消息“不幸的是,*已停止”我已经检查了其他关于此的 Q,但我发现没有任何帮助。我真的是android编码的新手......

这是我的 Java 代码:

package paket.accelerometer_axis;

import android.app.Activity;
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 accelerometer_axis extends Activity implements SensorEventListener{
private SensorManager sensorManager;
TextView xCoor; 
TextView yCoor; 
TextView zCoor; 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer_axis);

xCoor=(TextView)findViewById(R.id.xcoor); 
yCoor=(TextView)findViewById(R.id.ycoor); 
zCoor=(TextView)findViewById(R.id.zcoor); 

sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);}

public void onAccuracyChanged(Sensor sensor,int accuracy){
}

public void onSensorChanged(SensorEvent event){

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

float x=event.values[0];
float y=event.values[1];
float z=event.values[2];

xCoor.setText("X: "+x);
yCoor.setText("Y: "+y);
zCoor.setText("Z: "+z);
}}}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/back"
android:padding="4dip">

<ImageView
android:id="@+id/icon"
android:contentDescription="@string/description_logo"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher" 
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"

android:gravity="center_vertical"
android:text="@string/text0"
android:textColor="#00FF00"
android:textSize="20sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"

android:singleLine="true"
android:ellipsize="marquee"
android:text="@string/text1"
/>

<TextView
android:id="@+id/xcoor"
android:text="@string/text2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>

<TextView
android:id="@+id/ycoor"
android:text="@string/text3"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>

<TextView
android:id="@+id/zcoor"
android:text="@string/text4"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />

</LinearLayout>

这是我的清单:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />  

<uses-permission android:name="android.permission.INTERNET" />

<application          android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Accelerometer_axis"
        android:label="@string/title_activity_accelerometer_axis" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4

0 回答 0