package com.example.gameofguessing;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Screen extends Activity {
String lowerlimit1;
String upperlimit1;
int ulimit;
int llimit;
int count;
Intent nextscreen = Screen.this.getIntent();
{
lowerlimit1 = nextscreen.getStringExtra("llimit");
upperlimit1 = nextscreen.getStringExtra("ulimit");
// limit.setText(lowerlimit1);
llimit = Integer.parseInt(lowerlimit1);
ulimit = Integer.parseInt(upperlimit1);
}
Random random = new Random();
int randumnum = random.nextInt(ulimit - llimit + 1) + llimit;
Button go;
TextView limit;
EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
number = (EditText) findViewById(R.id.editText1);
go = (Button) findViewById(R.id.button1);
limit = (TextView) findViewById(R.id.textView1);
go.setOnClickListener(input);
}
OnClickListener input = new OnClickListener() {
@Override
public void onClick(View v) {
if(llimit <= randumnum) {
limit.setText("Too low");
number.setText("");
count = +1;
}
else if(llimit >= randumnum) {
limit.setText("Too high");
number.setText("");
count = +1;
}
else {
count = +1;
limit.setText("You got the number after" + count + "tries");
}
}
};
}
这是我到目前为止所得到的,我不知道为什么我的模拟器会继续崩溃。它与随机数或确定的意图有关。 请帮忙
答案 0 :(得分:2)
试试这个。问题是您在getIntent()
方法执行之前调用了onCreate
。因此,收到的意图尚未初始化和加载,因此您获得NullPointerException
。
package com.example.gameofguessing;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Screen extends Activity {
String lowerlimit1;
String upperlimit1;
int ulimit;
int llimit;
int count;
int randumnum
Button go;
TextView limit;
EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
number = (EditText) findViewById(R.id.editText1);
go = (Button) findViewById(R.id.button1);
limit = (TextView) findViewById(R.id.textView1);
go.setOnClickListener(input);
Intent nextscreen = Screen.this.getIntent();
lowerlimit1 = nextscreen.getStringExtra("llimit");
upperlimit1 = nextscreen.getStringExtra("ulimit");
// limit.setText(lowerlimit1);
llimit = Integer.parseInt(lowerlimit1);
ulimit = Integer.parseInt(upperlimit1);
Random random = new Random();
randumnum = random.nextInt(ulimit - llimit + 1) + llimit;
}
OnClickListener input = new OnClickListener() {
@Override
public void onClick(View v) {
if (llimit <= randumnum) {
limit.setText("Too low");
number.setText("");
count = +1;
} else if (llimit >= randumnum) {
limit.setText("Too high");
number.setText("");
count = +1;
} else {
count = +1;
limit.setText("You got the number after" + count + "tries");
}
}
};
}
答案 1 :(得分:0)
int randumnum = random.nextInt(ulimit - llimit + 1) + llimit;
将此行放在onCreate()
或您的任何一种方法中。因为类应该只包含变量和方法,所以所有定义都应该在方法中。这是一个定义。应该进入一个方法。