0

我在 Android 上的第一个应用程序有点问题。我一直在搜索有关如何使用传感器事件的所有信息,但是每次打开我的应用程序时我都会遇到 FC。

logcat 说:

02-26 05:45:55.291 I/ActivityManager(4935): Displayed me.das4ilor.fortunetellerfull/.MainActivity: +1s311ms
02-26 05:45:55.385 W/NetworkManagementSocketTagger(4935): setKernelCountSet(10056, 0) failed with errno -2
02-26 05:45:55.494 D/AndroidRuntime(19501): Shutting down VM
02-26 05:45:55.494 W/dalvikvm(19501): threadid=1: thread exiting with uncaught exception (group=0x40a031f8)
02-26 05:45:55.533 E/AndroidRuntime(19501): FATAL EXCEPTION: main
02-26 05:45:55.533 E/AndroidRuntime(19501): java.lang.NullPointerException
02-26 05:45:55.533 E/AndroidRuntime(19501):     at me.das4ilor.fortunetellerfull.MainActivity$1.onAccuracyChanged(MainActivity.java:58)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:574)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at android.os.Looper.loop(Looper.java:137)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at android.app.ActivityThread.main(ActivityThread.java:4575)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at java.lang.reflect.Method.invokeNative(Native Method)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at java.lang.reflect.Method.invoke(Method.java:511)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-26 05:45:55.533 E/AndroidRuntime(19501):     at dalvik.system.NativeStart.main(Native Method)
02-26 05:45:55.533 W/ActivityManager(4935):   Force finishing activity me.das4ilor.fortunetellerfull/.MainActivity

这是 .java :

package me.das4ilor.fortunetellerfull;

import me.das4ilor.fortunetellerfull.R;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;


public class MainActivity extends Activity {
public static final Random rgenerator = new Random();
private SensorManager mSensorManager;
private float mAccel;
private float mAccelCurrent;
private float mAccelLast; 
private TextView textView2;
private String[] myString;
Context context = this;

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

         Toast.makeText(context, "Thank you for buying full version!", Toast.LENGTH_SHORT).show();

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
    }



    private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta;
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

             myString =  getResources().getStringArray(R.array.String); 
             String q = myString[rgenerator.nextInt(myString.length)];
             textView2.setText(q);

        }
      };

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
      }

      @Override
      protected void onPause() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onPause();
      }

}
4

1 回答 1

0

您在 onAccuracyChanged 中有空指针异常(MainActivity.java 的第 58 行)。你有没有初始化过textView2?

于 2013-02-26T12:43:44.647 回答