我为Android编写了一个计算器,但是当我在模拟器中运行应用程序时按下“等于”按钮时,LogCat中出现错误,应用程序关闭。
请帮我修复错误。
这是LogCat输出:
07-25 01:34:13.213: I/Choreographer(1387): Skipped 39 frames! The application may be doing too much work on its main thread.
07-25 01:34:13.473: D/gralloc_goldfish(1387): Emulator without GPU emulation detected.
07-25 01:34:50.653: I/Choreographer(1387): Skipped 36 frames! The application may be doing too much work on its main thread.
07-25 01:34:52.973: D/AndroidRuntime(1387): Shutting down VM
07-25 01:34:52.973: W/dalvikvm(1387): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:34:53.103: E/AndroidRuntime(1387): FATAL EXCEPTION: main
07-25 01:34:53.103: E/AndroidRuntime(1387): Process: com.example.owncode, PID: 1387
07-25 01:34:53.103: E/AndroidRuntime(1387): java.lang.IllegalStateException: Could not find a method Sum(View) in the activity class com.example.owncode.MainActivity for onClick handler on view class android.widget.Button with id 'button_calculate'
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$1.onClick(View.java:3810)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View.performClick(View.java:4438)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.os.Looper.loop(Looper.java:136)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:34:53.103: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:34:53.103: E/AndroidRuntime(1387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:34:53.103: E/AndroidRuntime(1387): at dalvik.system.NativeStart.main(Native Method)
07-25 01:34:53.103: E/AndroidRuntime(1387): Caused by: java.lang.NoSuchMethodException: Sum [class android.view.View]
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.Class.getConstructorOrMethod(Class.java:472)
07-25 01:34:53.103: E/AndroidRuntime(1387): at java.lang.Class.getMethod(Class.java:857)
07-25 01:34:53.103: E/AndroidRuntime(1387): at android.view.View$1.onClick(View.java:3803)
07-25 01:34:53.103: E/AndroidRuntime(1387): ... 11 more
07-25 01:36:28.423: I/Choreographer(1452): Skipped 52 frames! The application may be doing too much work on its main thread.
07-25 01:36:28.753: D/gralloc_goldfish(1452): Emulator without GPU emulation detected.
07-25 01:36:32.873: I/Choreographer(1452): Skipped 39 frames! The application may be doing too much work on its main thread.
07-25 01:36:35.713: W/ResourceType(1452): No package identifier when getting value for resource number 0x00000009
07-25 01:36:35.723: D/AndroidRuntime(1452): Shutting down VM
07-25 01:36:35.723: W/dalvikvm(1452): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:36:36.103: E/AndroidRuntime(1452): FATAL EXCEPTION: main
07-25 01:36:36.103: E/AndroidRuntime(1452): Process: com.example.owncode, PID: 1452
07-25 01:36:36.103: E/AndroidRuntime(1452): java.lang.IllegalStateException: Could not execute method of the activity
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$1.onClick(View.java:3823)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View.performClick(View.java:4438)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.os.Looper.loop(Looper.java:136)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:36:36.103: E/AndroidRuntime(1452): at dalvik.system.NativeStart.main(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:36:36.103: E/AndroidRuntime(1452): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.view.View$1.onClick(View.java:3818)
07-25 01:36:36.103: E/AndroidRuntime(1452): ... 11 more
07-25 01:36:36.103: E/AndroidRuntime(1452): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.content.res.Resources.getText(Resources.java:244)
07-25 01:36:36.103: E/AndroidRuntime(1452): at android.widget.TextView.setText(TextView.java:3888)
07-25 01:36:36.103: E/AndroidRuntime(1452): at com.example.owncode.MainActivity.Sum(MainActivity.java:113)
07-25 01:36:36.103: E/AndroidRuntime(1452): ... 14 more
07-25 01:39:31.103: I/Choreographer(1502): Skipped 47 frames! The application may be doing too much work on its main thread.
07-25 01:39:31.383: D/gralloc_goldfish(1502): Emulator without GPU emulation detected.
07-25 01:39:35.203: I/Choreographer(1502): Skipped 42 frames! The application may be doing too much work on its main thread.
07-25 01:39:38.363: W/ResourceType(1502): No package identifier when getting value for resource number 0x00000009
07-25 01:39:38.423: D/AndroidRuntime(1502): Shutting down VM
07-25 01:39:38.423: W/dalvikvm(1502): threadid=1: thread exiting with uncaught exception (group=0xb3ab0ba8)
07-25 01:39:38.603: E/AndroidRuntime(1502): FATAL EXCEPTION: main
07-25 01:39:38.603: E/AndroidRuntime(1502): Process: com.example.owncode, PID: 1502
07-25 01:39:38.603: E/AndroidRuntime(1502): java.lang.IllegalStateException: Could not execute method of the activity
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$1.onClick(View.java:3823)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View.performClick(View.java:4438)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$PerformClick.run(View.java:18422)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Handler.handleCallback(Handler.java:733)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.os.Looper.loop(Looper.java:136)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-25 01:39:38.603: E/AndroidRuntime(1502): at dalvik.system.NativeStart.main(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): Caused by: java.lang.reflect.InvocationTargetException
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 01:39:38.603: E/AndroidRuntime(1502): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.view.View$1.onClick(View.java:3818)
07-25 01:39:38.603: E/AndroidRuntime(1502): ... 11 more
07-25 01:39:38.603: E/AndroidRuntime(1502): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.content.res.Resources.getText(Resources.java:244)
07-25 01:39:38.603: E/AndroidRuntime(1502): at android.widget.TextView.setText(TextView.java:3888)
07-25 01:39:38.603: E/AndroidRuntime(1502): at com.example.owncode.MainActivity.Sum(MainActivity.java:113)
07-25 01:39:38.603: E/AndroidRuntime(1502): ... 14 more
这是我的MainActivity.JAVA:
package com.example.owncode;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText calculatorText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculatorText = (EditText) findViewById(R.id.calculatorText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/*getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);*/
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.button_quitProgram) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void button_one_click(View view){
calculatorText.append("1");
}
public void button_two_click(View view){
calculatorText.append("2");
}
public void button_three_click(View view){
calculatorText.append("3");
}
public void button_four_click(View view){
calculatorText.append("4");
}
public void button_five_click(View view){
calculatorText.append("5");
}
public void button_six_click(View view){
calculatorText.append("6");
}
public void button_seven_click(View view){
calculatorText.append("7");
}
public void button_eight_click(View view){
calculatorText.append("8");
}
public void button_nine_click(View view){
calculatorText.append("9");
}
public void button_zero_click(View view){
calculatorText.append("0");
}
public void button_plus_click(View view){
calculatorText.append("+");
}
public void button_minus_click(View view){
calculatorText.append("-");
}
public void button_backspace_click(View view){
if (calculatorText.length()>0){
String calculatorText_text = calculatorText.getText().toString();
calculatorText_text = calculatorText_text.substring(0, calculatorText_text.length()-1);
calculatorText.setText(calculatorText_text);
}
}
public void Sum(View view){
String str = calculatorText.getText().toString();
int len = str.length();
int i=0;
String str1 = "", str2 = "";
char op;
int num1 = 0, num2 = 0, res=0;
for(i=0; i<len; i++){
if(str.charAt(i)=='+' || str.charAt(i)=='-')
break;
str1 = str1 + str.charAt(i);
}
op = str.charAt(i);
i++;
while(i<len){
str2 = str2 + str.charAt(i);
i++;
}
num1 = Integer.parseInt(str1);
num2 = Integer.parseInt(str2);
if(op=='+')
res = num1+num2;
else if(op=='-')
res = num1-num2;
calculatorText.setText(res);
}
}
而且,这是我的main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_minus"
android:layout_alignBottom="@+id/button_minus"
android:layout_alignParentLeft="true"
android:hint="@string/button_plus"
android:onClick="button_plus_click" />
<Button
android:id="@+id/button_zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_plus"
android:layout_alignBottom="@+id/button_plus"
android:layout_toRightOf="@+id/button_plus"
android:hint="@string/button_zero"
android:onClick="button_zero_click" />
<Button
android:id="@+id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="135dp"
android:hint="@string/button_minus"
android:onClick="button_minus_click" />
<Button
android:id="@+id/button_nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_minus"
android:layout_alignLeft="@+id/button_minus"
android:hint="@string/button_nine"
android:onClick="button_nine_click" />
<Button
android:id="@+id/button_eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_zero"
android:layout_toLeftOf="@+id/button_minus"
android:hint="@string/button_eight"
android:onClick="button_eight_click" />
<Button
android:id="@+id/button_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_eight"
android:layout_alignBottom="@+id/button_eight"
android:layout_toLeftOf="@+id/button_eight"
android:hint="@string/button_seven"
android:onClick="button_seven_click" />
<Button
android:id="@+id/button_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_nine"
android:layout_toRightOf="@+id/button_eight"
android:hint="@string/button_six"
android:onClick="button_six_click" />
<Button
android:id="@+id/button_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_six"
android:layout_alignBottom="@+id/button_six"
android:layout_toRightOf="@+id/button_seven"
android:hint="@string/button_five"
android:onClick="button_five_click" />
<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_five"
android:layout_alignBottom="@+id/button_five"
android:layout_toLeftOf="@+id/button_five"
android:hint="@string/button_four"
android:onClick="button_four_click" />
<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_six"
android:layout_toRightOf="@+id/button_five"
android:hint="@string/button_three"
android:onClick="button_three_click" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_three"
android:layout_alignBottom="@+id/button_three"
android:layout_toRightOf="@+id/button_four"
android:hint="@string/button_two"
android:onClick="button_two_click" />
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_two"
android:layout_alignBottom="@+id/button_two"
android:layout_toLeftOf="@+id/button_two"
android:hint="@string/button_one"
android:onClick="button_one_click" />
<Button
android:id="@+id/button_backspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_three"
android:layout_marginBottom="29dp"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/calculatorText"
android:hint="@string/button_backspace"
android:onClick="button_backspace_click" />
<Button
android:id="@+id/button_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_backspace"
android:layout_alignLeft="@+id/button_backspace"
android:hint="@string/button_calculate"
android:onClick="Sum" />
<EditText
android:id="@+id/calculatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button_backspace"
android:layout_alignParentLeft="true"
android:editable="false"
android:ems="10"
android:hint="@string/calculatorText_text" >
<requestFocus />
</EditText>
</RelativeLayout>
我知道这是一个简单的代码,但这是我的第一个应用程序,请帮助我,以便我可以在我的一些亲戚的设备上运行它。
答案 0 :(得分:2)
由于此行,您收到错误
calculatorText.setText(res);
将其更改为
calculatorText.setText(""+res);
您正在设置Integer
value..so查看将其视为Resouce ID
。
答案 1 :(得分:1)
将calculatorText.setText(res);
更改为calculatorText.setText(Integer.toString(res));
您不能将整数值设置为EditText。