Java 应用程序在启动时立即崩溃
我正在开发一个小的Android应用程序,让用户猜测1-100之间的数字,并显示猜测是否正确,是否太小或太高。它还在开发中,这是最基本的代码。 我想提到的另一件事是,我是一个Android初学者,这是我的第二个应用程序 活动\u main.xmlJava 应用程序在启动时立即崩溃,java,android,Java,Android,我正在开发一个小的Android应用程序,让用户猜测1-100之间的数字,并显示猜测是否正确,是否太小或太高。它还在开发中,这是最基本的代码。 我想提到的另一件事是,我是一个Android初学者,这是我的第二个应用程序 活动\u main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" and
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText android:id="@+id/number_input"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number" />
<Button android:id="@+id/guess_button"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/guess_button" />
</LinearLayout>
<TextView android:id="@+id/message_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp" />
</LinearLayout>
MainActivity.java
包com.vessialte.guessthenumber
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button guess_button;
TextView message_view;
EditText number_input;
int number;
int randNumber;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize views
guess_button = (Button) findViewById(R.id.guess_button);
message_view = (TextView) findViewById(R.id.message_view);
number_input = (EditText) findViewById(R.id.number_input);
number = Integer.parseInt(number_input.getText().toString());
// button click listener
guess_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (number < 1 || number > 100) {
AlertDialog.Builder errDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("The number has to be between 1 and 100")
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// temporary
Toast.makeText(getApplicationContext(), "OK button clicked", Toast.LENGTH_LONG).show();
}
});
errDialog.show();
} else {
r = new Random();
randNumber = r.nextInt(100) + 1;
if (number == randNumber) {
message_view.setText("Your guess was correct! The number is: " + randNumber);
} else if (number < randNumber) {
message_view.setText("Your guess was too low! The number is: " + randNumber);
} else if (number > randNumber) {
message_view.setText("Your guess was too high! The number is: " + randNumber);
}
}
}
});
}
}
import java.util.Random;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
按钮猜测按钮;
文本视图消息视图;
编辑文本编号\输入;
整数;
整数;
随机r;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化视图
guess_按钮=(按钮)findViewById(R.id.guess_按钮);
message_view=(TextView)findViewById(R.id.message_view);
数字输入=(EditText)findViewById(R.id.number\u输入);
number=Integer.parseInt(number_input.getText().toString());
//按钮单击侦听器
guess_button.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(数字<1 | |数字>100){
AlertDialog.Builder errDialog=新建AlertDialog.Builder(MainActivity.this)
.setTitle(“错误”)
.setMessage(“数字必须介于1和100之间”)
.setNeutralButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//暂时的
Toast.makeText(getApplicationContext(),“单击确定按钮”,Toast.LENGTH_LONG.show();
}
});
errDialog.show();
}否则{
r=新随机数();
随机数=r.nextInt(100)+1;
如果(编号==随机编号){
message_view.setText(“您的猜测是正确的!数字是:”+randNumber);
}否则如果(编号<随机编号){
message_view.setText(“您的猜测太低了!数字是:“+randNumber”);
}否则如果(编号>随机编号){
message_view.setText(“您的猜测太高了!数字是:“+randNumber”);
}
}
}
});
}
}
提前非常感谢。这一定是在OnClick Listener中
number = Integer.parseInt(number_input.getText().toString());
我想你得到了数字形式的感觉。因为最初Edittext值为空,并且您正在尝试转换为整数
guess_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
number = Integer.parseInt(number_input.getText().toString());
}
});
您甚至在填写任何内容之前都试图从
number\u输入中获取值。移动
number = Integer.parseInt(number_input.getText().toString());
在setOnClickListener()
中,只有当您单击guess_按钮时,才会要求android获取值,因此编辑文本中有一个值
请注意,当单击“猜测”按钮时,请进行检查以确保编辑文本不为空,否则您将再次出错。发布您的Java代码和日志会很有帮助。谢谢。还有一件事,EditText的类型是number,我无法检查它是否为空。你能帮我吗?在你的setOnClickListener()
中,检查如果(!number\u input.getText().toString().equals(“”)
@Vessilate如果我的答案解决了你的问题,请接受它并标记问题已解决。