简体   繁体   English

我在 Android 中面临 ClassNotFoundException

[英]I am facing ClassNotFoundException in Android

I'm trying to create a Chat app that uses Firebase.我正在尝试创建一个使用 Firebase 的聊天应用程序。 But suddenly I'm facing this error.但突然我面临这个错误。

02-13 10:23:02.533446   733   733 E AndroidRuntime: FATAL EXCEPTION: main
02-13 10:23:02.533446   733   733 E AndroidRuntime: Process: com.mycompany.myapp3, PID: 733
02-13 10:23:02.533446   733   733 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application com.mycompany.myapp3.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.mycompany.myapp3.MyApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp3-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp3-1/lib/arm, /system/lib, /vendor/lib, /system/vendor/lib]]
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.LoadedApk.makeApplication(LoadedApk.java:807)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5477)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.ActivityThread.-wrap2(ActivityThread.java)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.os.Looper.loop(Looper.java:203)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6251)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
02-13 10:23:02.533446   733   733 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mycompany.myapp3.MyApplication" on path: DexPathList[[zip file "/data/app/com.mycompany.myapp3-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mycompany.myapp3-1/lib/arm, /system/lib, /vendor/lib, /system/vendor/lib]]
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.Instrumentation.newApplication(Instrumentation.java:992)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     at android.app.LoadedApk.makeApplication(LoadedApk.java:801)
02-13 10:23:02.533446   733   733 E AndroidRuntime:     ... 9 more

build.gradle: build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.mycompany.myapp3"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    buildTypes {
        debugg {
            debuggable true
        }
    }
}

dependencies {
    implementation 'com.google.android.gms:play-services-analytics:15+'
    implementation 'com.google.firebase:firebase-storage:15+'
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.google.firebase:firebase-auth:15+'
    implementation 'com.google.firebase:firebase-database:15+'
}

AndroidManifest.xml: AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp3" >
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        
    <application
        
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".StartActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity" />
        <activity
            android:name=".ResetPasswordActivity" />
        <activity
            android:name=".LoginActivity" />
        <activity
            android:name=".MainActivity" />
        <activity
            android:name="ChatActivity" />
        <activity
            android:name=".UserProfileActivity" />
        <!--<activity
            android:name=".ImageActivity" />-->
        <activity
            android:name=".DebugActivity"
            android:screenOrientation="portrait"/>
        <meta-data
            android:name="android.max_aspect"
            android:value="4.0"/>
        
        <uses-library android:name="org.apache.http.legacy" android:required="false" />
    </application>

</manifest>

I don't know how Do I fix it.我不知道我该如何解决。 I will apprentice if you can help.如果你能帮忙,我会学徒。

NOTE: Please, I'm a beginner in this feild.注意:拜托,我是这个领域的初学者。 I really need help.我真的需要帮助。 If I missed to share any information, please let me know, I'll add the information.如果我错过了分享任何信息,请告诉我,我会添加信息。

Thanks.谢谢。

It looks like You have not registered your Application class in AndroidManifest file.看起来您尚未在AndroidManifest文件中注册您的应用程序 class。

Register like this像这样注册

<application
android:name=".MyApplication">

For android studio 4+, you need to enable Java 8 like this in your build.gradle (app) :对于 android studio 4+,您需要在build.gradle(应用程序)中启用 Java 8 像这样:

android {
    defaultConfig {
            ...
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM