应用程序在打开

时间:2016-05-29 20:08:41

标签: android android-fragments

嘿伙计们我刚刚进入Android的编程应用程序和我创建的第一个应用程序是一个简单的四功能计算器,它运行并在我的设备上安装得很好但是当我在我的设备上运行它时,它会强制停止。

我收到此消息:

05-29 12:48:52.065 20323-20323/com.awesomeapps.misael.simplecalculator E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: com.awesomeapps.misael.simplecalculator, PID: 20323
                                                                                         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

任何人都可以帮我解决这个问题吗?

任何帮助都会很棒!谢谢!

这是我的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=".Activity"
            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>

这是我的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;
        }
    }
}

抱歉我的帖子很长但我希望这有帮助:)提前感谢!

1 个答案:

答案 0 :(得分:-1)

您必须在Activity

中加入AndroidManifest.xml
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".Activity"
        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>
    <activity
        android:name=".MainActivity">
    </activity>

</application>

必须在那里宣布每项新活动。