我正在尝试构建一个计算器,问题似乎在于这个java代码。我尝试使用Android SDK的标准java代码并且布局有效。但是当我加载下面的代码时,却没有。我不明白为什么它不会加载。我是java新手,这让我很伤心。请帮忙。 (我把logcat放在底部。)
package com.haildark.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Calc extends Activity implements View.OnClickListener{
Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;
EditText disp;
int op1;
int op2;
String optr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calc);
one = (Button) findViewById(R.id.onebtn);
two = (Button) findViewById(R.id.twobtn);
three = (Button) findViewById(R.id.threebtn);
four = (Button) findViewById(R.id.fourbtn);
five = (Button) findViewById(R.id.fivebtn);
six = (Button) findViewById(R.id.sixbtn);
seven = (Button) findViewById(R.id.sevenbtn);
eight = (Button) findViewById(R.id.eightbtn);
nine = (Button) findViewById(R.id.ninebtn);
zero = (Button) findViewById(R.id.zerobtn);
add = (Button) findViewById(R.id.addbtn);
sub = (Button) findViewById(R.id.minusbtn);
mul = (Button) findViewById(R.id.multiplicationbtn);
div = (Button) findViewById(R.id.divisionbtn);
cancel = (Button) findViewById(R.id.cbtn);
equal = (Button) findViewById(R.id.equalbtn);
disp = (EditText) findViewById(R.id.editText1);
try{
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
zero.setOnClickListener(this);
cancel.setOnClickListener(this);
add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
equal.setOnClickListener(this);
}
catch(Exception e){
}
}
public void operation(){
if(optr.equals("+")){
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 + op2;
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("-")){
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 - op2;
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("*")){
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 * op2;
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("/")){
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 / op2;
disp.setText("Result : " + Integer.toString(op1));
}
}
@Override
public void onClick(View arg0) {
Editable str = disp.getText();
switch(arg0.getId()){
case R.id.onebtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(two.getText());
disp.setText(str);
break;
case R.id.twobtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(two.getText());
disp.setText(str);
break;
case R.id.threebtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(three.getText());
disp.setText(str);
break;
case R.id.fourbtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(four.getText());
disp.setText(str);
break;
case R.id.fivebtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(five.getText());
disp.setText(str);
break;
case R.id.sixbtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(six.getText());
disp.setText(str);
break;
case R.id.sevenbtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(eight.getText());
disp.setText(str);
break;
case R.id.eightbtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(nine.getText());
disp.setText(str);
break;
case R.id.ninebtn:
if(op2 != 0){
op2 = 0;
disp.setText("");
}
str = str.append(zero.getText());
disp.setText(str);
break;
case R.id.cbtn:
op1 = 0;
op2 = 0;
disp.setText("");
disp.setHint("Perform Operation :)");
break;
case R.id.addbtn:
optr = "+";
if(op1 == 0){
op1 = Integer.parseInt(disp.getText().toString());
disp.setText("");
}
else if(op2 != 0){
op2 = 0;
disp.setText("");
}
else{
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 + op2;
disp.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.minusbtn:
optr = "-";
if(op1 == 0){
op1 = Integer.parseInt(disp.getText().toString());
disp.setText("");
}
else if(op2 != 0){
op2 = 0;
disp.setText("");
}
else{
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 - op2;
disp.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.multiplicationbtn:
optr = "*";
if(op1 == 0){
op1 = Integer.parseInt(disp.getText().toString());
disp.setText("");
}
else if(op2 != 0){
op2 = 0;
disp.setText("");
}
else{
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 * op2;
disp.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.divisionbtn:
optr = "/";
if(op1 == 0){
op1 = Integer.parseInt(disp.getText().toString());
disp.setText("");
}
else if(op2 != 0){
op2 = 0;
disp.setText("");
}
else{
op2 = Integer.parseInt(disp.getText().toString());
disp.setText("");
op1 = op1 / op2;
disp.setText("Result : " + Integer.toString(op1));
}
break;
case R.id.equalbtn:
if(!optr.equals(null)){
if(op2 != 0){
if(optr.equals("+")){
disp.setText("");
/*op1 = op1 + op2;*/
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("-")){
disp.setText("");/*
op1 = op1 - op2;*/
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("*")){
disp.setText("");/*
op1 = op1 * op2;*/
disp.setText("Result : " + Integer.toString(op1));
}
else if(optr.equals("/")){
disp.setText("");/*
op1 = op1 / op2;*/
disp.setText("Result : " + Integer.toString(op1));
}
}
else{
operation();
}
}
break;
}
}
}
logcat的:
12-15 19:59:04.780: D/gralloc_goldfish(537): Emulator without GPU emulation detected.
12-15 19:59:04.840: W/TextLayoutCache(537): computeValuesWithHarfbuzz -- need to force to single run
12-15 19:59:05.800: D/AndroidRuntime(537): Shutting down VM
12-15 19:59:05.800: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-15 19:59:05.850: E/AndroidRuntime(537): FATAL EXCEPTION: main
12-15 19:59:05.850: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.haildark.calculator/com.one.slate.Calc}: java.lang.ClassNotFoundException: com.one.slate.Calc
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.os.Looper.loop(Looper.java:137)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 19:59:05.850: E/AndroidRuntime(537): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 19:59:05.850: E/AndroidRuntime(537): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 19:59:05.850: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 19:59:05.850: E/AndroidRuntime(537): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 19:59:05.850: E/AndroidRuntime(537): at dalvik.system.NativeStart.main(Native Method)
12-15 19:59:05.850: E/AndroidRuntime(537): Caused by: java.lang.ClassNotFoundException: com.one.slate.Calc
12-15 19:59:05.850: E/AndroidRuntime(537): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-15 19:59:05.850: E/AndroidRuntime(537): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-15 19:59:05.850: E/AndroidRuntime(537): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-15 19:59:05.850: E/AndroidRuntime(537): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
12-15 19:59:05.850: E/AndroidRuntime(537): ... 11 more
12-15 19:59:18.140: D/gralloc_goldfish(553): Emulator without GPU emulation detected.
12-15 19:59:18.181: W/TextLayoutCache(553): computeValuesWithHarfbuzz -- need to force to single run
12-15 19:59:19.191: D/AndroidRuntime(553): Shutting down VM
12-15 19:59:19.191: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-15 19:59:19.231: E/AndroidRuntime(553): FATAL EXCEPTION: main
12-15 19:59:19.231: E/AndroidRuntime(553): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.haildark.calculator/com.one.slate.Calc}: java.lang.ClassNotFoundException: com.one.slate.Calc
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:137)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-15 19:59:19.231: E/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 19:59:19.231: E/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 19:59:19.231: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-15 19:59:19.231: E/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-15 19:59:19.231: E/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
12-15 19:59:19.231: E/AndroidRuntime(553): Caused by: java.lang.ClassNotFoundException: com.one.slate.Calc
12-15 19:59:19.231: E/AndroidRuntime(553): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-15 19:59:19.231: E/AndroidRuntime(553): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-15 19:59:19.231: E/AndroidRuntime(553): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-15 19:59:19.231: E/AndroidRuntime(553): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
12-15 19:59:19.231: E/AndroidRuntime(553): ... 11 more
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.haildark.calculator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.haildark.calculator.Home"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.one.slate.Calc"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.one.slate.CALC" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
清单文件声明一个具有完全限定名称com.one.slate.Calc
的活动,但在代码中,类Calc
位于包com.haildark.calculator
中,因此完全限定名称为com.haildark.calculator.Calc
。尝试启动活动时,Android无法找到您的活动类。
要解决此问题,请更改清单类名称以匹配您的代码。