这个问题在这里已有答案:
我正在尝试使用API级别25在 avd 中运行该应用程序 .
起初,我创建了一个简单的空白活动并启动了应用程序,它的工作原理 . 然后我添加了传感器数据的代码,它崩溃了 .
这是我的 MainActivity.java :
public class MainActivity extends AppCompatActivity implements SensorEventListener{
private SensorManager sensorManager;
private Sensor accelerometer;
private long lastUpdate = 0;
private TextView x_val = (TextView)findViewById(R.id.x);
private TextView y_val = (TextView)findViewById(R.id.y);
private TextView z_val = (TextView)findViewById(R.id.z);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
if(sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
long curTime = System.currentTimeMillis();
if(curTime-lastUpdate > 100) //keep a difference of 100ms to avoid too much data
{
x_val.setText(x+"");
y_val.setText(y+"");
z_val.setText(z+"");
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onPause()
{
super.onPause();
sensorManager.unregisterListener(this);
}
public void onResume()
{
super.onResume();
sensorManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
}
}
这是 activity_main.xml :
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.naveen.accelerometer.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/x"
android:text="X : 0"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/y"
android:text="Y : 0"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/z"
android:text="Z : 0"
/>
在logcat中,我可以看到以下行 at com.example.naveen.accelerometer.MainActivity.(MainActivity.java:18)
完整logcat:
java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.naveen.accelerometer / com.example.naveen.accelerometer.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.Window $在Android.app的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)上的空对象引用上回调android.view.Window.getCallback()' .ActivityThread.-wrap12(ActivityThread.java)位于android.app.Loper.loop上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)android.os.Handler.dispatchMessage(Handler.java:102) (Looper.java:154)位于android.app.ActivityThread.main(ActivityThread.java:6119)的java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:886)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)引起:java.lang.NullPointerException:A尝试在android.support的android.support.v7.app.AppCompatDelegateImplBase . (AppCompatDelegateImplBase.java:120)上的空对象引用上调用虚方法'android.view.Window $ Callback android.view.Window.getCallback()' .v7.app.AppCompatDelegateImplV9 . (AppCompatDelegateImplV9.java:155)位于android.support.v7.app.AppCompatDelegateImplV11 . (AppCompatDelegateImplV11.java:31),位于android.support.v7.app.AppCompatDelegateImplV14 . (AppCompatDelegateImplV14.java:55)位于android.support.v7.app.AppCompatDelegateImplN的android.support.v7.app.AppCompatDelegateImplV23 . (AppCompatDelegateImplV23.java:33) . (android.com.uugate.dev . ) . (AppCompatDelegateImplN.Java:33),位于android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate) .java:201)在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)android.support.v7上的android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) . com.example.naveen.accelerometer上的app.AppCompatActivity.findViewById(AppCompatActivity.java:190) .MainActivity . (MainActivity.java:18)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java)的android.app.Instrumentation.newActivity(Instrumentation.java:1078)的java.lang.Class.newInstance(Native Method)中 . 2557)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)android.app.ActivityThread.-wrap12(ActivityThread.java)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)at android位于android.app.Looper.loop(Looper.java:154)的.os.Handler.dispatchMessage(Handler.java:102)位于java.lang.reflect的android.app.ActivityThread.main(ActivityThread.java:6119) . 在Com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)上的Method.invoke(Native Method)