2017-03-11 48 views
0

我试图做一个简单的计算器,但我得到一个错误。这是我的代码。循环内arraylist中的错误java.lang.IllegalStateException:无法执行android的方法:onClick

public class MainActivity extends AppCompatActivity { 
TextView tvInput; 
String textcontent = ""; 
String input = ""; 
ArrayList<Integer> number = new ArrayList<Integer>(); 
ArrayList<Character> operator = new ArrayList<Character>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tvInput= (TextView) findViewById(R.id.textView_input); 
} 


public void btn1(View view) { 
    input = input + "1"; 
    tvInput.setText(input); 
} 

public void btn3(View view) { 
    input = input + "3"; 
    tvInput.setText(input); 
} 

public void btn2(View view) { 
    input = input + "2"; 
    tvInput.setText(input); 
} 


public void btn0(View view) { 
    input = input + "0"; 
    tvInput.setText(input); 
} 

public void btn7(View view) { 
    input = input + "7"; 
    tvInput.setText(input); 
} 

public void btn8(View view) { 
    input = input + "8"; 
    tvInput.setText(input); 
} 
public void btn9(View view) { 
    input = input + "9"; 
    tvInput.setText(input); 
} 
public void btn5(View view) { 
    input = input + "5"; 
    tvInput.setText(input); 
} 
public void btn6(View view) { 
    input = input + "6"; 
    tvInput.setText(input); 
} 
public void btnSub(View view) { 
    number.add(Integer.parseInt(tvInput.getText().toString())); 
    tvInput.setText("-"); 
    operator.add('-'); 
    input = ""; 
} 
public void btnMul(View view) { 
    number.add(Integer.parseInt(tvInput.getText().toString())); 
    tvInput.setText("*"); 
    operator.add('*'); 
    input = ""; 
} 
public void btnDiv(View view) { 
    number.add(Integer.parseInt(tvInput.getText().toString())); 
    tvInput.setText("/"); 
    operator.add('/'); 
    input = ""; 
} 
public void btn4(View view) { 
    input = input + "4"; 
    tvInput.setText(input); 
} 
public void btnAdd(View view) { 
    number.add(Integer.parseInt(tvInput.getText().toString())); 
    tvInput.setText("+"); 
    operator.add('+'); 
    input = ""; 
} 
public void btnEqual(View view) { 
    int result = 0; 
    for(int i = 1; i <= number.size(); i++) { 
     result = number.get(i - 1); 
     if(operator.get(i - 1) == '+') 
     result = result+number.get(i); //error is here 
     tvInput.setText("" + result); 
    } 
    } 
} 

我认为错误的,因为循环中的结果变化VAR ..

这是错误:

java.lang.IllegalStateException: Could not execute method for android:onClick

问题出在哪里? 解决方案是什么?

+1

我在代码中看不到任何onClick方法;它可能是必需的。也许考虑发布整个堆栈跟踪,而不是你认为可能至关重要的部分 – planetmaker

+0

请附上你的整个错误堆栈跟踪。但我有一种感觉,问题在于你连接xml文件中按钮的onclick。你应该有'onClick =“btn1()”'' – Siavash

回答

0

您在下面的代码得到错误,因为ArrayIndexOutofBound最后指数

result=result+number.get(i); 

在for循环中你已经盯着我形成1,通过i<=number.size()和你正在使用取值(i-1),但在在上面你直接取值,所以当循环到达最后的位置时,它没有最后一个元素。使用下面的代码来完成这个操作。

public void btnEqual(View view) { 
    int result=0; 
    for(int i=0;i<number.size();i++){ 
     result=number.get(i); 
     if(operator.get(i)=='+') 
     result=result+number.get(i); //error is here 
     tvInput.setText(""+result); 
    } 
    } 
} 
相关问题