【问题标题】:Android - Unable to instantiate activityAndroid - 无法实例化活动
【发布时间】:2013-12-27 19:21:42
【问题描述】:

我目前正在使用 Android Studio 开发我自己的 Android 项目(我的自定义启动器)!问题是,自上次更新(至 0.4.0 版)以来,我无法运行我的应用程序。

相反,我收到以下错误:

FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.natercio.myhome/com.natercio.myhome.Launcher}: java.lang.ClassNotFoundException: Didn't find class "com.natercio.myhome.Launcher" on path: DexPathList[[zip file "/data/app/com.natercio.myhome-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.natercio.myhome-2, /system/lib]]
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            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:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.natercio.myhome.Launcher" on path: DexPathList[[zip file "/data/app/com.natercio.myhome-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.natercio.myhome-2, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)

我到处搜索我能记得的解决方案(或相关的东西)并尝试了一堆东西,但没有任何成功。

  • 检查“compiler.xml”(没有排除项)
  • 清理/构建(错误仍然存​​在)
  • 检查“AndroidManifest.xml”(一切似乎都正确)
  • 重新创建了整个项目(只导入了代码和资源,但错误仍然存​​在)

我注意到这一行的一件事

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.natercio.myhome.Launcher" on path: DexPathList[[zip file "/data/app/com.natercio.myhome-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.natercio.myhome-2, /system/lib]]

是不是有一个“-2”(有时是一个“-1”)附加到应用程序的名称(在 apk 文件和 lib 路径中),但我之前从未真正注意到这一点,所以我可以'不确定这是否与我的问题有关。

还请考虑到我使用 git,并且我已经尝试将任何更改恢复到我以前稳定的提交之一。

更新:

还有其他人在 Android Studio 中遇到过这个问题吗?是否有任何其他解决方案可以解决我错过的此类错误,或者这可能是最新的 android 插件(适用于 android studio)中的一些错误。

更新(build.gradle 和清单文件):

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 18
    buildToolsVersion '18.1.1'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:support-v13:+'
    compile 'com.j256.ormlite:ormlite-android:4.47'
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.natercio.myhome"
          android:versionName="@string/app_version_name">

    <uses-sdk android:targetSdkVersion="17"
              android:minSdkVersion="15"/>

    <uses-permission android:name="android.permission.SET_WALLPAPER"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.Launcher">

        <service android:name=".profiler.ProfilerService"
                 android:exported="false"/>

        <activity android:name=".Launcher"
                  android:screenOrientation="portrait"
                  android:launchMode="singleTask"
                  android:description="@string/app_description">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.HOME"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

        </activity>

    </application>

</manifest>

【问题讨论】:

  • 您能否发布更多关于您的项目布局的详细信息,并包含您的 build.gradle 文件?
  • @ScottBarta 我已经包含了更多细节!如果您需要更多详细信息,请直接说出来
  • 更新到 0.4.0 后我遇到了同样的问题。每次更新 Android Studio 时,我都想知道为什么我将它用于生产工作。 :)

标签: java android android-studio updates


【解决方案1】:

在将 Android Studio 更新到 0.4.0 后,我遇到了同样的问题。这似乎过于简单,但我通过简单地删除我的整个 /build 文件夹(在 0.4.0 更新之前创建的那个)并重建来解决了这个问题。

【讨论】:

    【解决方案2】:

    试试这个 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo / java.lang.ClassNotFoundException

    或者, 使用 CMD 访问项目并清理您的项目

    清理干净

    gradlew 构建

    【讨论】:

      【解决方案3】:

      我相信 -2 或 -1 来自拥有多个副本?至少在我的经验中是这样的。但是您的问题到底是什么?

      【讨论】:

      • 我已经更新了帖子!你说“-#”代表多个副本,但具体是什么和在哪里(设备或开发机器)
      • 开发机器。如果你没有把它放在那里。该程序可能对多个同名文件使用某种类型的命名约定。
      【解决方案4】:

      错误表明您在项目中使用的库之一有多个副本。进入 project structure -> modules 并检查 target 或 apk-libs 文件夹,如果 android studio 正在提取多个副本。我在 android studio 中遇到了同样的错误,结果发现 studio 在 target 文件夹中生成了一个名为 unpack 的新文件夹,这导致了问题。

      【讨论】:

      • 我在您提到的菜单中找不到任何相关内容!相反,我得到了这些选项卡:属性、签名、风味、构建类型和依赖项(最后一个选项卡中没有重复的条目)
      • 分发地址是什么?
      • distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
      【解决方案5】:

      在对 android studio 进行一些更新后问题自行解决了!

      【讨论】:

        【解决方案6】:

        检查您的项目设置->模块->您的项目名称->来源

        确保您的 src 和 res 列表在右侧框架上(命名为 Add Content Root,res 作为资源文件夹,src 作为 Sources 文件夹)

        或者你的项目遇到一些错误,愚蠢的 android studio 仍然在你的设备上放了错误的 apk 并且运行当然得到了错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多