应用重新启动,由于AndroidRuntime异常而无法运行

时间:2018-07-28 06:37:35

标签: java android xml android-studio android-gradle

Gradle构建成功,logcat显示E / AndroidRuntime:FATAL EXCEPTION:main

-------- Logcat错误-----------

07-27 23:04:54.813 21702-21702/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.inoalexander.justjava, PID: 21702
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.inoalexander.justjava/com.example.inoalexander.justjava.mainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.inoalexander.justjava.mainActivity" on path: DexPathList[[zip file "/data/app/com.example.inoalexander.justjava-2/base.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.inoalexander.justjava-2/lib/arm64, /system/lib64, /vendor/lib64]]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2819)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6682)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.inoalexander.justjava.mainActivity" on path: DexPathList[[zip file "/data/app/com.example.inoalexander.justjava-2/base.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.inoalexander.justjava-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.inoalexander.justjava-2/lib/arm64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6682) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424) 

--------- Java代码从此处开始------------

package com.example.inoalexander.justjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
* This app displays an order form to order coffee.
*/
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView)            findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="vertical"
tools:context=".MainActivity">

--------- XML代码从此处开始-------------

<TextView

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="16sp"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="16dp"
    android:text="Quantity" />
<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="16sp"
    android:layout_marginLeft="16dp"
    android:layout_marginBottom="16dp"
    android:text="0" />
<Button
    android:id="@+id/button_id"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="Order"
    android:onClick="submitOrder"/>




</LinearLayout>

我不确定,因为这是我开发的第一批不是卡片的应用程序之一。我不确定为什么它不能在手机上运行。我正在使用Galaxy Note 5运行我的版本。

Gradle构建成功,并且没有给我任何错误,但是当我尝试在设备上运行它时,它一直尝试重新启动应用程序,直到最终导致应用程序崩溃。我在这里查看了一些答案,并被指示在android studio中查看我的Logcat

老实说,我什至不知道是从Logcat开始还是什至告诉我,我看到的这个错误有很多变化,但我的{ {1}}。

2 个答案:

答案 0 :(得分:0)

Go to Project > General > Deployment Info 设置为适用于应用程序级别multidex的文件,如下所示

build.gradle

还添加依赖项

android {
    compileSdkVersion 27
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.core.######"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true // here change
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
}

最后不要忘了清理和重建项目。

答案 1 :(得分:0)

该异常表明它正在尝试查找类mainActivity,但是在您的代码中却有MainActivity

确保AndroidManifest.xml中的活动类名称与您的代码匹配。