2013-06-24 198 views
0

我试图写一个代码,以使一个简单的计算器,但模拟器不工作!它给了我这个消息“对不起,应用Task_6(过程com.example.task_6)已意外停止,请重试。”动态按钮onClickListener

这是来自logcat的味精: “在dalvik.system.NativeStart。主(本地梅托德)”当我点击按钮(+)或退格键:\\

布局是一样的东西CAL窗口:

这是我的代码,我不知道是否有不对的代码:\!

package com.example.task_6; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity implements OnClickListener { 

    EditText textBox; 
    Button b1,b2,b3,b4,b5,b6,b7; 
    int number_1,number_2,result,temp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     textBox= (EditText) findViewById(R.id.TextBox); 
     b1=(Button)findViewById(R.id.button1); 
     b2=(Button)findViewById(R.id.Button01); 
     b3=(Button)findViewById(R.id.button2); 
     b4=(Button)findViewById(R.id.button3); 
     b5=(Button)findViewById(R.id.button4); 
     b6=(Button)findViewById(R.id.button5); 
     b7=(Button)findViewById(R.id.button6); 

     b1.setOnClickListener(this); 
     b2.setOnClickListener(this); 
     b3.setOnClickListener(this); 
     b4.setOnClickListener(this); 
     b5.setOnClickListener(this); 
     b6.setOnClickListener(this); 
     b7.setOnClickListener(this); 
     } 

    public void onClick(View arg) { 
    if(arg==b1){ 

     textBox.setText(""); 

    } 

if(arg==b2){ 
    temp=Integer.parseInt(textBox.getText().toString().substring(0, textBox.length()-1)); 
    textBox.setText(temp); 
    } 
if(arg==b3){ 

    number_1=Integer.parseInt(textBox.getText().toString()); 
    textBox.setText(""); 
    number_2=Integer.parseInt(textBox.getText().toString()); 
    result=number_1+number_2; 
} 

if(arg==b4){ 

    number_1=Integer.parseInt(textBox.getText().toString()); 
    textBox.setText(""); 
    number_2=Integer.parseInt(textBox.getText().toString()); 
    result=number_1-number_2; 
} 

if(arg==b5){ 
    textBox.setText(result); 
} 
if(arg==b6){ 

    number_1=Integer.parseInt(textBox.getText().toString()); 
    textBox.setText(""); 
    number_2=Integer.parseInt(textBox.getText().toString()); 
    result=number_1*number_2; 
} 
if(arg==b7){ 

    number_1=Integer.parseInt(textBox.getText().toString()); 
    textBox.setText(""); 
    number_2=Integer.parseInt(textBox.getText().toString()); 
    result=number_1/number_2; 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

enter image description here

+0

发表您的错误日志吗? – juned

+0

阅读logcat消息,如果您无法解决问题,请发布错误消息 –

+1

单击b5时是否出现错误? – Blackbelt

回答

2

问题上用onclick声明

如果你使用:

public void function(View arg) 
{ 
    .... 
    .... 
} 

那么就应该从布局XML本身被调用。

XML文件:

<button 
     android:id="@+id/b1" 
     android:onclick="function" /> 

当你动态创建的onClick函数,那么它就像:

b1.setOnClickListener(new onClickListener(
{ 
    public void onClick(View v) 
    { 
     .... 
     .... 
    } 
}); 
+0

是否存在这种代码的问题:temp = Integer.parseInt(textBox.getText()。toString()。substring(0,textBox.length() - 1)); \t \t textBox.setText(temp); – Akari

+0

,因为如果输入123,那么按下退格键,编辑文本的值应该是12,如果你不输入任何值,按下这个按钮时模拟器会停止 – Akari

+1

?即.. '字符串名称= “”;' 'name.subString(0,-1);' 程序将崩溃.. 这是一个错误。 发布您的LogCat ..它会显示确切的错误 –