[英]What is wrong with my code for a number guessing game?
package hjroh0315.numbergame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int SelN=-1;
Random Rand = new Random();
TextView Hint = (TextView)findViewById(R.id.textView);
EditText GuessInput = (EditText)findViewById(R.id.editText);
Button NewNumber = (Button)findViewById(R.id.button);
Button Guess = (Button)findViewById(R.id.button2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NewNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SelN = Rand.nextInt(100);
}
});
Guess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Guessed = GuessInput.getText().toString();
int Num = Integer.parseInt(Guessed);
if(SelN == -1)Hint.setText("Error: Number not generated.");
else if (Num < SelN)
{
Hint.setText("Up");
}
else if(Num > SelN)
{
Hint.setText("Down");
}
else
{
Hint.setText("Good");
SelN=-1;
}
}
});
}
}
When I run this code, The app crashed instantly. 当我运行此代码时,该应用程序立即崩溃。 It says there is an error in line 15. but I can not understand why. 它说第15行有错误,但我不明白为什么。 can anyone please explain this to me? 有人可以向我解释一下吗? PS This is for a number guessing game which is a project of mine. PS这是一个数字猜谜游戏,属于我的项目。
You should no initialize textview outside onCreate method: 您不应该在onCreate方法之外初始化textview:
TextView Hint = (TextView)findViewById(R.id.textView);
The layout is not inflated yet, so it cannot find it 布局尚未膨胀,因此找不到它
You should copy all your findViewById in the onCreate method after 您应该在之后的onCreate方法中复制所有findViewById
setContentView();
Try like this: 尝试这样:
public class MainActivity extends AppCompatActivity {
int SelN=-1;
Random Rand = new Random();
TextView Hint;
EditText GuessInput;
Button NewNumber;
Button Guess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Hint = (TextView)findViewById(R.id.textView);
GuessInput = (EditText)findViewById(R.id.editText);
NewNumber = (Button)findViewById(R.id.button);
Guess = (Button)findViewById(R.id.button2);
NewNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SelN = Rand.nextInt(100);
}
});
Guess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Guessed = GuessInput.getText().toString();
int Num = Integer.parseInt(Guessed);
if(SelN == -1)Hint.setText("Error: Number not generated.");
else if (Num < SelN)
{
Hint.setText("Up");
}
else if(Num > SelN)
{
Hint.setText("Down");
}
else
{
Hint.setText("Good");
SelN=-1;
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.