嗨,我很新的android。我認爲第一個應用的好主意是計算器。我創建了該應用程序完全安裝並編譯正確。但是當我在設備上運行它時,它強制關閉。我認爲在我的AndroidManifest.xml文件中可能存在問題,但我不知道它是什麼。AndroidManifes xml的問題。 App部隊在運行中關閉
這是可以將Java代碼:
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.awesomeapps.misael.simplecalculator.R;
public class MainActivity 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.content_main);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
zero = (Button) findViewById(R.id.zero);
add = (Button) findViewById(R.id.add);
sub = (Button) findViewById(R.id.sub);
mul = (Button) findViewById(R.id.mul);
div = (Button) findViewById(R.id.div);
cancel = (Button) findViewById(R.id.cancel);
equal = (Button) findViewById(R.id.equal);
disp = (EditText) findViewById(R.id.editText);
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.one:
if (op2 != 0) {
op2 = 0;
disp.setText("");
}
str = str.append(two.getText());
disp.setText(str);
break;
case R.id.two:
if (op2 != 0) {
op2 = 0;
}
str = str.append(two.getText());
disp.setText(str);
case R.id.three:
if (op2 != 0) {
op2 = 0;
}
str = str.append(three.getText());
disp.setText(str);
break;
case R.id.four:
if (op2 != 0) {
op2 = 0;
}
str = str.append(four.getText());
disp.setText(str);
break;
case R.id.five:
if (op2 != 0) {
op2 = 0;
}
str = str.append(five.getText());
disp.setText(str);
break;
case R.id.six:
if (op2 != 0) {
op2 = 0;
}
str = str.append(six.getText());
disp.setText(str);
break;
case R.id.seven:
if (op2 != 0) {
op2 = 0;
}
str = str.append(eight.getText());
disp.setText(str);
break;
case R.id.eight:
if (op2 != 0) {
op2 = 0;
}
str = str.append(nine.getText());
disp.setText(str);
break;
case R.id.nine:
if (op2 != 0) {
op2 = 0;
}
str = str.append(zero.getText());
disp.setText(str);
break;
case R.id.cancel:
op1 = 0;
op2 = 0;
disp.setText("");
disp.setHint("Perform Operation");
break;
case R.id.add:
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.sub:
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.mul:
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.div:
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.equal:
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;
}
}
}
這是我AndroidManifest XML文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.awesomeapps.misael.simplecalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
哦,我也收到此錯誤信息:
05-29 21:48:03.091 10728-10728/com.awesomeapps.misael.simplecalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.awesomeapps.misael.simplecalculator, PID: 10728
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.awesomeapps.misael.simplecalculator/com.awesomeapps.misael.simplecalculator.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.awesomeapps.misael.simplecalculator.MainActivity" on path: DexPathList[[zip file "/data/app/com.awesomeapps.misael.simplecalculator-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.awesomeapps.misael.simplecalculator.MainActivity" on path: DexPathList[[zip file "/data/app/com.awesomeapps.misael.simplecalculator-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Suppressed: java.lang.ClassNotFoundException: com.awesomeapps.misael.simplecalculator.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 13 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
如果任何人都可以請通過告訴我要在我的Java代碼或Android清單xml文件中修復哪些內容來提供幫助。或者,也許它完全不同。
任何幫助將是真棒,非常感謝!謝謝! :)
在你的activity類中沒有'package com.awesomeapps.misael.simplecalculator;'。 – Ufkoku
具有'MainActivity'類的包的名稱是什麼?我想你把不正確的包裹。 –
添加軟件包並刪除'''import com.awesomeapps.misael.simplecalculator.R;''' –