2016-11-14 31 views
-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 
       } 
     ); 


    } 

} 

任何幫助,將不勝感激。

+0

我從教程中獲得的大部分代碼。 – RuberDucky

+0

在setContentView() – Raghavendra

+0

初始化之後,在onCreate中進行初始化意味着所有findViewById以及使用這些視圖的任何地方。在setContentView語句 – Raghavendra

回答

0

1)將你的//獲取信息和//按鈕創建者部分放在setcretentview語句之後的oncreate函數中。

2)// //處理數據部分應該在檢查按鈕內點擊。請包括與該部分的另一個檢查是否edittext內容爲空以避免空崩潰。