-1

我正在尝试让计时器工作,开始和停止按钮点击事件。我似乎在某个地方引用了空指针,但我在我的代码中找不到问题。如果有人可以帮助我,那就太好了。这是我不断收到的 logcat 错误。我在我的 AndroidManifest 中添加了活动。

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hva.ktv.getconnected/hva.ktv.getconnected.TestActivity}:java.lang.NullPointerException

public class TestActivity extends Activity 
{

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

    a.setOnClickListener(mStartListener);
    b.setOnClickListener(mStopListener);
}

private Handler mHandler = new Handler();
private long mStartTime;
TextView mTimeLabel = (TextView) findViewById(R.id.textView1);
Button a = (Button) findViewById(R.id.button1);
Button b = (Button) findViewById(R.id.button2);

OnClickListener mStartListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mStartTime == 0L) {
            mStartTime = System.currentTimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }
    }

};

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        final long start = mStartTime;
        long millis = SystemClock.uptimeMillis() - start;
        int seconds = (int) (millis / 1000);
        int minutes = seconds / 60;
        seconds = seconds % 60;

        if (seconds < 10) {
            mTimeLabel.setText("" + minutes + ":0" + seconds);
        } else {
            mTimeLabel.setText("" + minutes + ":" + seconds);
        }

        mHandler.postAtTime(this, start
                + (((minutes * 60) + seconds + 1) * 1000));
    }
};

OnClickListener mStopListener = new OnClickListener() {
    public void onClick(View v) {
        mHandler.removeCallbacks(mUpdateTimeTask);
    }
};

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

}
4

1 回答 1

0

您在 onCreate() 方法中有以下内容:

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

    a.setOnClickListener(mStartListener);
    b.setOnClickListener(mStopListener);
}

关于那段代码有两个问题,第一个是你正在为按钮 a 和 b 设置一个监听器,如果你不给它们充气,它们就会到达 null,通过调用findViewById(int res). 但这实际上不是你的问题,看看这段代码,它在进入onCreate(bundle)方法之前执行:

Button a = (Button) findViewById(R.id.button1);
Button b = (Button) findViewById(R.id.button2);
TextView mTimeLabel = (TextView) findViewById(R.id.textView1);

现在,问题很简单,您尝试在调用 setContentView(int layout) 之前获取这些按钮和 TextView,这实际上会为您提供这些视图。因此,修复代码很容易,只需用这个替换你的 onCreate(bundle) 方法,(记得删除你膨胀小部件的字段):

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

    TextView mTimeLabel = (TextView) findViewById(R.id.textView1);
    Button a = (Button) findViewById(R.id.button1);
    Button b = (Button) findViewById(R.id.button2);

    a.setOnClickListener(mStartListener);
    b.setOnClickListener(mStopListener);
}
于 2013-06-08T22:06:15.650 回答