Android - RuntimeException,哪里出错?

时间:2013-05-26 09:32:53

标签: android

在这个应用程序中,我有3个按钮和1个textView。 在现阶段,我基本上留下了很多空白。 例如我还没有实现按钮功能。

但现在内部存在运行时错误。 甚至元素都无法显示出来。

你能帮忙吗?

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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

    Button startButton;
    Button pauseButton;
    Button resetButton;
    TextView textView = (TextView) findViewById(R.id.textView1);

    private SensorManager sensorManager;
    private long lastUpdate;

    private boolean isCalibrated = false;
    private int referrenceAxis;
    private int stepCounts = 0;

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

        // start button
        startButton = (Button) findViewById(R.id.button1);
        startButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            }
        });

        // pause button
        pauseButton = (Button) findViewById(R.id.button2);
        pauseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            }
        });

        // reset button
        resetButton = (Button) findViewById(R.id.button3);
        resetButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

            }
        });

        init();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void init() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        lastUpdate = System.currentTimeMillis();
    }

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
            long actualTime = System.currentTimeMillis();

            if (isCalibrated && (actualTime - lastUpdate > 500)) {
                if (event.values[referrenceAxis] > 1) {
                    stepCounts++;
                }
            }

        }

        if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
            if (!isCalibrated) {

                if ((event.values[0] >= event.values[1])&&(event.values[0] >= event.values[2]))
                    referrenceAxis = 0;
                if ((event.values[1] >= event.values[0])&&(event.values[1] >= event.values[2]))
                    referrenceAxis = 1;
                if ((event.values[2] >= event.values[0])&&(event.values[2] >= event.values[1]))
                    referrenceAxis = 2;

                isCalibrated = true;
            }
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    protected void onResume() {
        super.onResume();
        // register this class as a listener for the sensors
        sensorManager
                .registerListener(this, sensorManager
                        .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
                        SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        // unregister listener
        super.onPause();
        sensorManager.unregisterListener(this);
    }

}

1 个答案:

答案 0 :(得分:4)

使用

TextView textView = (TextView) findViewById(R.id.textView1);

之后

    setContentView(R.layout.activity_main);

在充气父版面之前,不能给子视图充气。因此,在您的情况下,将textView声明为成员变量并在setContentView之后初始化它。所以你的实现就像

作为成员变量声明

TextView textView;
创建

setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView1);