-5
你好我是新來的android工作室,並希望你們中的一個人/ gals可以幫我解釋爲什麼我的代碼會導致應用程序崩潰?該代碼是一個簡單的數學難題,你必須輸入正確的數字才能得到答案。 代碼:尋找與編碼問題的幫助我有
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
}
);
}
}
任何幫助,將不勝感激。
我從教程中獲得的大部分代碼。 – RuberDucky
在setContentView() – Raghavendra
初始化之後,在onCreate中進行初始化意味着所有findViewById以及使用這些視圖的任何地方。在setContentView語句 – Raghavendra