Android错误中的基本计算器

时间:2016-10-22 15:28:57

标签: java android android-studio

活动主要:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

}

public void calc(View view){


    EditText E1=(EditText)findViewById(R.id.txtfno);
    EditText E2=(EditText)findViewById(R.id.txtsno);

    //reads 1st value
    int val1 = Integer.parseInt(E1.getText().toString());
    int val2 = Integer.parseInt(E2.getText().toString());
    int result = 0;


    //checking which button was click
    if(view.getId()==R.id.button6)
    {
        result=val1+val2;
        TextView tt = (TextView)findViewById(R.id.txtresult);
        tt.setText(String.valueOf(result));
    }
    if(view.getId()==R.id.button7)
    {
        result=val1-val2;
        TextView tt = (TextView)findViewById(R.id.txtresult);
        tt.setText(String.valueOf(result));
    }
    if(view.getId()==R.id.button5)
    {
        result=val1val2;
        TextView tt = (TextView)findViewById(R.id.txtresult);
        tt.setText(String.valueOf(result));
    }
    if(view.getId()==R.id.button8)
    {
        result=val1/val2;
        TextView tt = (TextView)findViewById(R.id.txtresult);
        tt.setText(String.valueOf(result));
       }

      }
     }

主要活动Java

Error:(15, 22) error: cannot find symbol class View
Error:(18, 9) error: cannot find symbol class EditText
Error:(18, 22) error: cannot find symbol class EditText
Error:(19, 9) error: cannot find symbol class EditText
Error:(19, 22) error: cannot find symbol class EditText
Error:(31, 13) error: cannot find symbol class TextView
Error:(31, 28) error: cannot find symbol class TextView
Error:(37, 13) error: cannot find symbol class TextView
Error:(37, 28) error: cannot find symbol class TextView
Error:(42, 20) error: cannot find symbol variable val1val2
Error:(43, 13) error: cannot find symbol class TextView
Error:(43, 28) error: cannot find symbol class TextView
Error:(49, 13) error: cannot find symbol class TextView
Error:(49, 28) error: cannot find symbol class TextView

以下是错误:

lst

2 个答案:

答案 0 :(得分:1)

您需要为:

添加导入
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

另外,我不确定您当前的方法是否有效。如果没有,请使用findViewById单独获取每个按钮,并将onClickListener设置为它们。

要检查单击了哪个按钮,请使用:

Button btn1 = (Button) findViewById(R.id.button5);
Button btn2 = (Button) findViewById(R.id.button6);
Button btn3 = (Button) findViewById(R.id.button7);
Button btn4 = (Button) findViewById(R.id.button8);
TextView tt = (TextView)findViewById(R.id.txtresult);
int result;

btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        result = val1*val2
    }
});
btn2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        result = val1+val2
    }
});
btn3.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        result = val1-val2
    }
});
btn4.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        result = val1/val2
    }
});
tt.setText(result+"");

希望我帮助过:D

答案 1 :(得分:0)

您应该在calc方法

中调用onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    calc(yourview);
}