嘿伙计们,我对android很新。我认为第一个应用程序的好主意是计算器。我完全安装并正确编译了该应用程序。但是,当我在我的设备上运行时,它会强行关闭。我想我的AndroidManifest.xml文件可能存在问题,但我不知道它是什么。
这可能是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 manifest xml文件中修复什么来取悦。或许它的东西完全不同。
任何帮助都会很棒,非常感谢!谢谢! :)
答案 0 :(得分:0)
在 MainActivity 类的顶部添加以下行:
package com.awesomeapps.misael.simplecalculator;
在此之后清理并重建您的项目。
答案 1 :(得分:0)
我试图运行您的代码并将所有内容粘贴到新项目中。我接下来做的是:
1)删除了这个
import com.awesomeapps.misael.simplecalculator.R;
2)将我的包导入MainActivity.java的第一行(我猜你的情况就是这样)
package com.awesomeapps.misael.simplecalculator;
我没有触及清单文件。然后它结果运行没有任何错误。
但是,我应该提一下代码中有很多错误。至少,它没有显示结果,按下它们时某些数字按钮输入的值不正确。运行代码并修复错误。最后,如果您在布局中包含一些可解释如何使用计算器的textview,那就太棒了。快乐的编码:)
答案 2 :(得分:0)
将包名添加到MainActivity
<?= $form->field($model, 'color', [
'template' => "{input}"
])->input('color',['class'=>"input_class"]) ?>
删除此行package com.awesomeapps.misael.simplecalculator;
删除构建文件夹 清理并重建您的项目。希望它能解决你的问题。
答案 3 :(得分:-2)
如果您想在项目中启用multi-dex,请转到gradle.builder
并在您的依赖项中添加此内容
dependencies {
compile 'com.android.support:multidex:1.0.0'}
然后你必须添加
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true}
然后打开一个类并将其扩展到Application如果你的应用程序使用扩展Application类,你可以覆盖oncrete()方法并调用。
//add this code into your application class
public class MyApplication extends Application {
// public Context context;
@Override
public void onCreate( ) {
super.onCreate();
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
MultiDex.install(this);
}
启用multidex。
最后加入你的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name=".MyApplication"">
...
</application>
</manifest>