【问题标题】:Basic Calculator in Android errorsAndroid错误中的基本计算器
【发布时间】:2016-10-22 15:28:57
【问题描述】:

活动主要:

  <TextView
    android:text="Enter your first number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/textView2"
    android:textSize="20sp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_marginTop="12dp"
    android:id="@+id/txtfno"
    android:layout_below="@+id/textView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text="Enter your second number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:textSize="20sp"
    android:layout_below="@+id/txtfno"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="16dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:layout_below="@+id/textView3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="17dp"
    android:id="@+id/txtsno" />

<TextView
    android:text="Answer:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtsno"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="13dp"
    android:id="@+id/result"
    android:textSize="20sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/result"
    android:layout_toRightOf="@+id/result"
    android:layout_toEndOf="@+id/result"
    android:layout_marginLeft="15dp"
    android:layout_marginStart="15dp"
    android:id="@+id/txtresult"
    android:textSize="20sp" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/result"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="38dp">

     <Button
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button6"
        android:layout_weight="1"
        android:onClick="calc"/>

    <Button
        android:text="-"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button7"
        android:layout_weight="1"
        android:onClick="calc"/>

    <Button
        android:text="*"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_weight="1"
        android:onClick="calc"/>

    <Button
        android:text="/"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button8"
        android:layout_weight="1"
        android:onClick="calc"/>
</LinearLayout>

</RelativeLayout>

主要活动 Java

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));
       }

      }
     }

以下是错误:

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

【问题讨论】:

  • 您需要导入您的小部件(View、EditText 和 TextView)!

标签: java android android-studio


【解决方案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

【讨论】:

  • 是的,仍然出现错误。我还是 android 新手 错误:(21, 9) 错误: 找不到符号类 EditText 错误:(21, 22) 错误: 找不到符号类 EditText 错误:(22, 9) 错误: 找不到符号类 EditText 错误: (22, 22) 错误: 找不到符号类 EditText 错误:(45, 20) 错误: 找不到符号变量 val1val2
【解决方案2】:

你应该在onCreate方法中调用calc方法

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

    calc(yourview);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    相关资源
    最近更新 更多