2

我对 Android 编程很陌生。

我试图编写几个代码,但我陷入了加速度计程序。将程序加载到模拟器后,它会响应Adil_acceleration_3rd已停止”

我为您提供我的文件:

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/acceleration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="X: Y: Z:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

这是我的ActivityMain.java文件:

package com.example.adil_acceleration_3rd;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity implements SensorEventListener{

    Sensor accelerometer;
    SensorManager sm;
    TextView acceleration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sm=(SensorManager)getSystemService(SENSOR_SERVICE);
        accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        acceleration.setText("X:"+event.values[0]+
                "\nY:"+event.values[1]+
                "\nZ:"+event.values[2]);
    }

}

LogCat 消息:

03-16 13:26:41.210: E/Trace(1926): error opening trace file: No such file or directory (2)
03-16 13:26:41.210: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.210: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.210: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.280: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.280: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.871: W/Trace(1926): Unexpected value from nativeGetEnabledTags: 0
03-16 13:26:41.871: D/AndroidRuntime(1926): Shutting down VM
03-16 13:26:41.871: W/dalvikvm(1926): threadid=1: thread exiting with uncaught exception (group=0xb4d31908)
03-16 13:26:41.880: E/AndroidRuntime(1926): FATAL EXCEPTION: main
03-16 13:26:41.880: E/AndroidRuntime(1926): java.lang.NullPointerException
03-16 13:26:41.880: E/AndroidRuntime(1926):     at com.example.adil_acceleration_3rd.MainActivity.onSensorChanged(MainActivity.java:46)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at android.os.Looper.loop(Looper.java:137)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 13:26:41.880: E/AndroidRuntime(1926):     at dalvik.system.NativeStart.main(Native Method)
03-16 13:27:04.801: I/Process(1926): Sending signal. PID: 1926 SIG: 9
4

1 回答 1

2

在这些情况下,请仔细阅读 logcat。大多数情况下,它会给出一个很好的提示或至少给出问题的位置。

就您而言,重要的几行在这里:

03-16 13:26:41.880: E/AndroidRuntime(1926): java.lang.NullPointerException 
03-16 13:26:41.880: E/AndroidRuntime(1926): at com.example.adil_acceleration_3rd.MainActivity.onSensorChanged(MainActivity.java:46) 

这是这一行:

acceleration.setText("X:"+event.values[0]+

发生这种情况是因为acceleration已声明,但从未初始化。

PS 对于您的下一个问题,请格式化 logcat 输出以便于阅读。

于 2013-03-16T18:14:40.943 回答