I am trying to run the app in avd
with API level 25.
At first, I created a simple blank activity and started the app, it works. Then I added the code for Sensor data, it crashes.
This is my 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);
}
}
This is activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/x"
android:text="X : 0"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/y"
android:text="Y : 0"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/z"
android:text="Z : 0"
/>
</LinearLayout>
In logcat, I can see the following line at com.example.naveen.accelerometer.MainActivity.<init>(MainActivity.java:18)
Full logcat :
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.naveen.accelerometer/com.example.naveen.accelerometer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:120) at android.support.v7.app.AppCompatDelegateImplV9.(AppCompatDelegateImplV9.java:155) at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:31) at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:55) at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:33) at android.support.v7.app.AppCompatDelegateImplN.(AppCompatDelegateImplN.java:33) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) at com.example.naveen.accelerometer.MainActivity.(MainActivity.java:18) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)