Android Studio代码编译但不会运行

时间:2016-05-20 08:48:05

标签: java android

使用加速度计制作计步器。 代码编译时没有错误,但是当我在手机上运行它时(S5)它会失败。

主要活动代码

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) 

日志中的错误..

{{1}}

任何帮助表示感谢,如果问题非常简单,我很抱歉 谢谢

1 个答案:

答案 0 :(得分:2)

setContentView()

之后初始化您的textview
 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);

        }

g的值在onSensorChanged()初始化,因此在该方法中设置:

    @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);

    }