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");
}
}
};
}
这就是到目前为止,我不知道为什么我的模拟器不断崩溃。肯定与随机数或意图有关。
请帮忙
最佳答案
尝试这个。问题在于您正在执行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");
}
}
};
}