我正在编写一个显示加速度计读数的应用程序,我昨天能够在主要活动中编写所有加速度计代码,它在模拟器和我的设备上运行良好。
但是今天我试图制作一个包含所有加速度计代码的函数,并返回整数数组中的三个整数X,Y,Z,所以我创建了一个代码,我认为它是正确的,但每次运行时项目模拟器给出错误“遗憾的是app-name已停止”。
所以我希望得到任何帮助。
logcat的:
04-27 20:39:27.098: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-27 20:39:27.118: E/AndroidRuntime(812): FATAL EXCEPTION: main
04-27 20:39:27.118: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accelerometer_sensor/com.example.accelerometer_sensor.MainActivity}: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 20:39:27.118: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
04-27 20:39:27.118: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.AccelerometerClass.AccelerometerInit(AccelerometerClass.java:20)
04-27 20:39:27.118: E/AndroidRuntime(812): at com.example.accelerometer_sensor.MainActivity.onCreate(MainActivity.java:19)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Activity.performCreate(Activity.java:5104)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-27 20:39:27.118: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-27 20:39:27.118: E/AndroidRuntime(812): ... 11 more
04-27 20:39:30.119: I/Process(812): Sending signal. PID: 812 SIG: 9
这是我的主要活动代码:
package com.example.accelerometer_sensor;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
int [] AccVal = new int[3];
AccelerometerClass acc = new AccelerometerClass();
TextView acceleration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acc = new AccelerometerClass();
AccVal = acc.AccelerometerInit();
acceleration = (TextView)findViewById(R.id.acceleration);
acceleration.setText("X: "+AccVal[0]+
"Y: "+AccVal[1]+
"Z: "+AccVal[2]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是我的AccelerometerClass Code ::
package com.example.accelerometer_sensor;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class AccelerometerClass implements SensorEventListener
{
private static Context context;
int [] Vals = new int[3];
public int [] AccelerometerInit()
{
Sensor accelerometer;
SensorManager sm;
sm = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
Vals[0]=(int)event.values[0];
Vals[1]=(int)event.values[1];
Vals[2]=(int)event.values[2];
}
}
谢谢。
答案 0 :(得分:1)
border-radius:1px
中的上下文永远不会设置!
可能的解决方案:
AccelerometerClass
在您的活动中使用
public class AccelerometerClass implements SensorEventListener {
int[] Vals = new int[3];
public int[] AccelerometerInit(Context context) {
SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
return Vals;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
Vals[0] = (int) event.values[0];
Vals[1] = (int) event.values[1];
Vals[2] = (int) event.values[2];
}
}