0

Making a step counter with the accelerometer. Code compiles with no errors but when i run it on the phone(S5) it fails.

CODE IN MAIN ACTIVITY

public class MainActivity extends Activity implements SensorEventListener{

        TextView display;
        float g;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display = (TextView) findViewById(R.id.testView); // Correction
        SensorManager mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor GSensor = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mgr.registerListener(this, GSensor, SensorManager.SENSOR_DELAY_NORMAL);

        String mytext = Float.toString(g);

        display.setText(mytext);


    }
    @Override
    public void onSensorChanged(SensorEvent event){

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        g = (x * x + y * y + z *z)/(SensorManager.GRAVITY_EARTH *SensorManager.GRAVITY_EARTH);



    }



    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy){
    }
}

ERRORS IN LOG..

05-20 09:35:44.773 11142-11142/com.liamoc.stepcounteraccel1 E/Zygote: MountEmulatedStorage()
05-20 09:35:44.773 11142-11142/com.liamoc.stepcounteraccel1 E/Zygote: v2
05-20 09:35:44.773 11142-11142/com.liamoc.stepcounteraccel1 I/libpersona: KNOX_SDCARD checking this for 10199
05-20 09:35:44.773 11142-11142/com.liamoc.stepcounteraccel1 I/libpersona: KNOX_SDCARD not a persona
05-20 09:35:44.793 11142-11142/com.liamoc.stepcounteraccel1 I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G900F_5.0-1_0032
05-20 09:35:44.793 11142-11142/com.liamoc.stepcounteraccel1 E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
05-20 09:35:44.793 11142-11142/com.liamoc.stepcounteraccel1 I/art: Late-enabling -Xcheck:jni
05-20 09:35:44.823 11142-11142/com.liamoc.stepcounteraccel1 D/TimaKeyStoreProvider: TimaSignature is unavailable
05-20 09:35:44.823 11142-11142/com.liamoc.stepcounteraccel1 D/ActivityThread: Added TimaKeyStore provider
05-20 09:35:44.863 11142-11142/com.liamoc.stepcounteraccel1 D/ResourcesManager: creating new AssetManager and set to /data/app/com.liamoc.stepcounteraccel1-2/base.apk
05-20 09:35:44.883 11142-11142/com.liamoc.stepcounteraccel1 W/art: Failed to find OatDexFile for DexFile /data/data/com.liamoc.stepcounteraccel1/files/instant-run/dex/slice-slice_6-classes.dex ( canonical path /data/data/com.liamoc.stepcounteraccel1/files/instant-run/dex/slice-slice_6-classes.dex) with checksum 0x1ff97ab0 in OatFile /data/data/com.liamoc.stepcounteraccel1/cache/slice-slice_6-classes.dex
05-20 09:35:45.053 11142-11142/com.liamoc.stepcounteraccel1 D/AndroidRuntime: Shutting down VM
05-20 09:35:45.053 11142-11142/com.liamoc.stepcounteraccel1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.liamoc.stepcounteraccel1, PID: 11142
                                                                              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamoc.stepcounteraccel1/com.liamoc.stepcounteraccel1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2515)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:172)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:145)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
                                                                                  at android.app.Activity.findViewById(Activity.java:2168)
                                                                                  at com.liamoc.stepcounteraccel1.MainActivity.<init>(MainActivity.java:29)
                                                                                  at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                  at java.lang.Class.newInstance(Class.java:1650)
                                                                                  at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:172) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:145) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5832) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Any help appreciated, im a noob so sorry ifthe prob is really simple.. Thanks

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
iamliam
  • 1
  • 1
  • Whatever you are loading textview is wrong.You should call textview after loading of your View XML. Textview should be called after `setContentView` – Madhukar Hebbar May 20 '16 at 08:51
  • `findViewById()` should be used after `setContentView()` Also you'll get `NullPointerException` on .`String mytext = Float.toString(g);` as `g` is not initialized. – SachinGutte May 20 '16 at 08:54
  • Perfect..I got it to run. Thanks alot! Its not counting the steps tho..ill try figure it out tho and post back if i cant get it going, thanks again.. – iamliam May 20 '16 at 08:57

1 Answers1

2

Initialize your textview after setContentView()

 TextView display;

     @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            display = (TextView) findViewById(R.id.testView);

            SensorManager mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
            Sensor GSensor = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            mgr.registerListener(this, GSensor, SensorManager.SENSOR_DELAY_NORMAL);

        }

Value of g is initializating in onSensorChanged(), so set it in that method:

    @Override
    public void onSensorChanged(SensorEvent event){

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        g = (x * x + y * y + z *z)/(SensorManager.GRAVITY_EARTH *SensorManager.GRAVITY_EARTH);

       String mytext = Float.toString(g);
       display.setText(mytext);

    }
Hello World
  • 2,764
  • 1
  • 11
  • 23