私は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