由于测量时间,Nullpointerexception?

时间:2015-03-09 14:40:08

标签: java android button nullpointerexception

我想测量第一次按键点击和第三次按键点击之间的时间。当我在设备上第三次点击Button时,代码本身工作正常,我得到了nullpointerexception。

为什么?

这是代码:

public class Game extends Activity implements OnClickListener{

Button start_time;

int i = 0;
TextView textview1;

Button RelativeLayout;
Button gameover;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//  RelativeLayout.setClickable(true);
    setContentView(R.layout.activity_game);
      start_time = (Button) findViewById(R.id.start_time);
      start_time.setOnClickListener(this);
      textview1 = (TextView) findViewById(R.id.textView1);
      gameover = (Button) findViewById(R.id.gameover);


      gameover.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                finish();


            }


            });



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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override


public void onClick(View v) {

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

    Random r = new Random();
    RelativeLayout decorView = (RelativeLayout) start_time.getParent();
    int screenWidth = decorView.getWidth();
    int screenHeight = decorView.getHeight();
      long startTime =  SystemClock.elapsedRealtime();
      i++;


    if (i == 1 ) {

        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 2 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 3 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 4 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 5 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    if (i == 6 ) {
        start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
        start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
    }
    else if (i == 7) {
        long difference = SystemClock.elapsedRealtime()  - startTime;

          Intent intent = new Intent(Game.this, MainScreen.class);
          intent.putExtra("time",difference);

        // textview1.setText(intent.getStringExtra("time"));
      //   textview1.setText(String.valueOf(difference));
        finish();
    }


}

}

和主要活动:

public class MainScreen extends Activity implements OnClickListener {

Button btn_start;
TextView textview1;




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


    btn_start = (Button) findViewById(R.id.btn_start);
    btn_start.setOnClickListener(this);
    textview1 = (TextView) findViewById(R.id.textView1);

}

@Override
public void onClick(View v) {
    startActivityForResult(new Intent(this,Game.class), 0);
    textview1.setText(getIntent().getStringExtra("time"));  
}

}

logcat的:

03-09 16:08:10.760: E/AndroidRuntime(6372): FATAL EXCEPTION: main
03-09 16:08:10.760: E/AndroidRuntime(6372): Process: ch.frozensparks.asdf, PID: 6372
03-09 16:08:10.760: E/AndroidRuntime(6372): java.lang.NullPointerException
03-09 16:08:10.760: E/AndroidRuntime(6372):     at ch.frozensparks.bftjh.Game.onClick(Game.java:118)
03-09 16:08:10.760: E/AndroidRuntime(6372):     at android.view.View.performClick(View.java:4470)
03-09 16:08:10.760: E/AndroidRuntime(6372):     at android.view.View$PerformClick.run(View.java:18593)
03-09 16:08:10.760: E/AndroidRuntime(6372):     at android.os.Handler.handleCallback(Handler.java:733)
03-09 16:08:10.760: E/AndroidRuntime(6372):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-09 16:08:10.760: E/AndroidRuntime(6372):     at android.os.Looper.loop(Looper.java:157)

3 个答案:

答案 0 :(得分:1)

此行发生例外

textview1.setText(getIntent().getStringExtra("time"));

您正在尝试从当前活动获取内容但在此处传递给新的意图对象

Intent intent = new Intent(Game.this, MainScreen.class);
          intent.putExtra("time",difference);

答案 1 :(得分:1)

(首先,我假设帖子顶部的代码是Game.class的一部分。)

好的,我认为你误解了意图的运作方式。

Intent intent = new Intent(Game.this, MainScreen.class);
    intent.putExtra("time", difference);

在这里,你要创建一个新的意图(称为'意图')并将价值与差异放在一起'用钥匙'时间'在里面。

然后

    textview1.setText(getIntent().getStringExtra("time"));

您在此尝试访问该值 - 但您实际上正在访问完全不同的意图对象。通过使用getIntent(),您可以访问用于启动当前活动的intent对象,而不是您在其上面定义的intent对象。 (你也试图从意图中获得一个额外的字符串,但是你要存储一个长的。)

如果要访问该intent对象,只需使用:

intent.getStringExtra("time");

所以你的行将是:

textview1.setText(intent.getStringExtra("time"));

但我会这样做:

textview1.setText(String.valueOf(difference));

最后,我无法确定您在顶部代码块中定义textview1的位置,但是如果您尝试引用可能是MainScreen的一部分的textview1导致您的问题,因此您可能必须将其公开或传递差异变量并在MainScreen中设置文本。

答案 2 :(得分:-1)

导航到游戏时,您需要完成yoir MainScreen Activity。      MainScreen.this.finish();在MainScreen的onClick方法中。

此外,您需要在游戏活动的onclick中启动活动

startActivity(intent)