嗨,我是一名初学Android开发人员,试图制作基本的计算器应用
这是我在content_main.xml文件夹中的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">
<TextView
android:id="@+id/displayer"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/minus"
android:onClick="subtract"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/displayer"
android:layout_toStartOf="@+id/displayer"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="+"
android:id="@+id/plus"
android:layout_below="@+id/minus"
android:layout_alignLeft="@+id/minus"
android:layout_alignStart="@+id/minus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="setNumber"
android:tag="1"
android:id="@+id/one"
android:layout_above="@+id/plus"
android:layout_toRightOf="@+id/plus"
android:layout_toEndOf="@+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="4"
android:id="@+id/four"
android:tag="4"
android:layout_alignTop="@+id/plus"
android:layout_toRightOf="@+id/plus"
android:layout_toEndOf="@+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="2"
android:id="@+id/two"
android:tag="2"
android:layout_above="@+id/four"
android:layout_toRightOf="@+id/one"
android:layout_toEndOf="@+id/one" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="3"
android:tag="3"
android:id="@+id/three"
android:layout_above="@+id/five"
android:layout_toRightOf="@+id/two"
android:layout_toEndOf="@+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="5"
android:tag="5"
android:id="@+id/five"
android:layout_below="@+id/two"
android:layout_alignLeft="@+id/two"
android:layout_alignStart="@+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="6"
android:tag="6"
android:id="@+id/six"
android:layout_above="@+id/nine"
android:layout_toRightOf="@+id/five"
android:layout_toEndOf="@+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="7"
android:tag="7"
android:id="@+id/seven"
android:layout_below="@+id/plus"
android:layout_toRightOf="@+id/plus"
android:layout_toEndOf="@+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="8"
android:tag="8"
android:id="@+id/eight"
android:layout_below="@+id/five"
android:layout_alignLeft="@+id/five"
android:layout_alignStart="@+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="9"
android:tag="9"
android:id="@+id/nine"
android:layout_below="@+id/five"
android:layout_alignLeft="@+id/six"
android:layout_alignStart="@+id/six" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/equal"
android:onClick="equal"
android:layout_below="@+id/plus"
android:layout_alignLeft="@+id/plus"
android:layout_alignStart="@+id/plus" />
</RelativeLayout>
以下是MainActivity文件夹中的Activity:
package com.example.android.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private int firstNumber = -1;
private int secondNumber = -1;
private int operation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void equal(View view){
TextView displayerTextView=(TextView)
findViewById(R.id.displayer);
if (operation == 0) {
displayerTextView.setText(firstNumber+secondNumber);
}else if (operation == 1){
displayerTextView.setText(firstNumber-secondNumber);
}else{
displayerTextView.setText("error");
}
}
private void add(View view){
operation = 0;
}
private void subtract(View view){
operation = 1;
}
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
}
当我点击计算器应用程序中的3按钮时,此处显示的错误是错误:
java.lang.IllegalStateException: Could not find a method setNumber(View) in the activity class com.example.android.calculator.MainActivity for onClick handler on view class android.widget.Button with id 'three'
答案 0 :(得分:1)
因为您的方法是private
。更改public
。
您可以在此处找到更多http://developer.android.com/reference/android/widget/Button.html
答案 1 :(得分:0)
应该是 public void setNumber(View view){
答案 2 :(得分:0)
对于上述问题,请更改
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
到
public void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
要避免使用加减法在代码中出现类似错误,请将这些函数从private
更改为public
。