找不到活动类异常

时间:2017-03-11 05:58:25

标签: java android android-manifest classnotfoundexception

我正在尝试在Android 18的旧设备上运行一个Android项目。该项目在Android> 21上工作正常,但在Android 18上我收到以下错误:

  

03-11 01:22:31.147 2343-2608 /? E /看门狗:!@Sync 466 03-11   01:23:01.152 2343-2608 /? E / Watchdog:!@Sync 467 03-11 01:23:16.602   24016-24016 /? E / AndroidRuntime:致命异常:主要   java.lang.RuntimeException:无法实例化活动   ComponentInfo {com。示例/ com.example.MainActivity}:   java.lang.ClassNotFoundException:没找到类   路径上的“com.example.MainActivity”:/ data / app / com.example-1.apk at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)   在android.app.ActivityThread.access $ 700(ActivityThread.java:159)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)   在android.os.Handler.dispatchMessage(Handler.java:99)at   android.os.Looper.loop(Looper.java:176)at   android.app.ActivityThread.main(ActivityThread.java:5419)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:525)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)at   dalvik.system.NativeStart.main(Native方法)引起:   java.lang.ClassNotFoundException:没找到类   路径上的“com.example.MainActivity”:/ data / app / com.example-1.apk at   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)   在java.lang.ClassLoader.loadClass(ClassLoader.java:501)at   java.lang.ClassLoader.loadClass(ClassLoader.java:461)at   android.app.Instrumentation.newActivity(Instrumentation.java:1078)at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)   在android.app.ActivityThread.access $ 700(ActivityThread.java:159)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1316)   在android.os.Handler.dispatchMessage(Handler.java:99)at   android.os.Looper.loop(Looper.java:176)at   android.app.ActivityThread.main(ActivityThread.java:5419)at   java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:525)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)at   dalvik.system.NativeStart.main(Native Method)03-11 01:23:17.052   2343年至2441年/? E / android.os.Debug:!@ Rollstate> sdumpstate -k -t -z -d   -o / data / log / dumpstate_app_error 03-11 01:23:23.087 2698-2698 /? E / Launcher:找不到设置,找不到默认的辅助功能:   accessibility_enabled 03-11 01:23:23.147 2343-2441 /? E / ViewRootImpl:   sendUserActionEvent()mView == null 03-11 01:23:23.162 2343-4810 /?   E / EnterpriseContainerManager:ContainerPolicy服务还没有   准备!!! 03-11 01:23:23.377 24110-24132 /? E / Babel:canonicalizeMccMnc:   无效mccmnc 03-11 01:23:23.377 24110-24132 /? E /巴贝尔:   canonicalizeMccMnc:invalid mccmnc nullnull 03-11 01:23:31.152   2343年至2608年/? E /看门狗:!@Sync 468

我的Android清单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

为什么不能找到主类?如何使该应用与Android 18兼容?

编辑1: 我在MainActivity中尝试了addiong multidex功能:

public class MainActivity extends AppCompatActivity implements IResults {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
...

在gradle中:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {
    compileSdkVersion 24
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 18
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        // Enabling multidex support.
        multiDexEnabled true
    }
...

dependencies {
    // Generic dependencies
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:design:24.2.0'

    // Support libraries
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"
    compile "com.android.support:design:$supportLibraryVersion"
    compile "com.android.support:cardview-v7:$supportLibraryVersion"
    compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
    compile 'com.android.support:multidex:1.0.1'
...

但仍然没有运气。还有其他想法如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您必须使用MultidexApplication

<application
        android:name="android.support.multidex.MultiDexApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

或将MultiDex.install(this);放入Application班级