你好我是Android工作室的新手,并希望你们其中一个人/女孩可以帮我解释为什么我的代码会让应用程序崩溃?代码用于简单的数学难题,您必须输入正确的数字才能得到答案。 代码:
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Puzzle extends AppCompatActivity {
//get the info
EditText input01 = ( EditText ) findViewById ( R.id.input1 );
EditText input02 = ( EditText ) findViewById ( R.id.input2 );
EditText input03 = ( EditText ) findViewById ( R.id.input3 );
EditText input04 = ( EditText ) findViewById ( R.id.input4 );
EditText input05 = ( EditText ) findViewById ( R.id.input5 );
EditText input06 = ( EditText ) findViewById ( R.id.input6 );
EditText input07 = ( EditText ) findViewById ( R.id.input7 );
EditText input08 = ( EditText ) findViewById ( R.id.input8 );
EditText input09 = ( EditText ) findViewById ( R.id.input9 );
//process data
int ans01 = Integer.valueOf ( input01.getText().toString() );
int ans02 = Integer.valueOf ( input02.getText().toString() );
int ans03 = Integer.valueOf ( input03.getText().toString() );
int ans04 = Integer.valueOf ( input04.getText().toString() );
int ans05 = Integer.valueOf ( input05.getText().toString() );
int ans06 = Integer.valueOf ( input06.getText().toString() );
int ans07 = Integer.valueOf ( input07.getText().toString() );
int ans08 = Integer.valueOf ( input08.getText().toString() );
int ans09 = Integer.valueOf ( input09.getText().toString() );
//button creater
Button check = ( Button ) findViewById(R.id.check);
Button reset = ( Button ) findViewById(R.id.Reset);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
reset.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick ( View view ) {
input01.setText ( "" );
input02.setText ( "" );
input03.setText ( "" );
input04.setText ( "" );
input05.setText ( "" );
input06.setText ( "" );
input07.setText ( "" );
input08.setText ( "" );
input09.setText ( "" );
}
}
);
// this is where the code starts crashing the app.
check.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
// I want an if statement that checks the values of the inputed numbers
if ( ans01 + ans02 + ans03 = 14) {
input01.setTextColor(Color.GREEN);
input02.setTextColor(Color.RED);
}
}enter code here
}
);
}
}
任何帮助将不胜感激。
答案 0 :(得分:0)
1)在//创建onconate函数中的setcontentview语句后,将//获取info和//按钮创建器部分。
2)另外//进程数据部分应该在里面单击检查按钮。请在该部分包含另一项检查,以确保edittext内容为空以避免空崩溃。