简体   繁体   English

Android-无法实例化活动

[英]Android - Unable to instantiate activity

I'm currently using Android Studio to develop my own project for Android (my custom launcher)! 我目前正在使用Android Studio开发自己的Android项目(我的自定义启动器)! The thing is, since last update (to version 0.4.0) I can't run my app. 事实是,自从上次更新(版本为0.4.0)以来,我无法运行我的应用程序。

Instead I get the following error: 相反,我得到以下错误:

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)

I've searched everywhere I could remember for a solution (or something related) and tried a bunch of stuff already without any success. 我到处搜寻了我能记得的解决方案(或相关内容),并尝试了很多东西,但都没有成功。

  • check the "compiler.xml" (there are no excludes) 检查“ compiler.xml”(不排除)
  • clean/build (error persists) 清理/构建(错误仍然存​​在)
  • check the "AndroidManifest.xml" (everything seems correct) 检查“ AndroidManifest.xml”(一切似乎正确)
  • recreated the whole project (only imported the code and the resources but the error persists) 重新创建了整个项目(仅导入了代码和资源,但错误仍然存​​在)

One thing I did notice in this line 我在这一行中注意到的一件事

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]]

is that there is an "-2" (and sometimes a "-1") appended to the name of the app (both in the apk file and in the lib path) but I never really noticed this before so I can't tell for sure if this is somehow related with my problem. 是在应用程序名称(在apk文件和lib路径中)后面附加了“ -2”(有时是“ -1”),但是我之前从未真正注意到这一点,所以我无法分辨确定这是否与我的问题有关。

Please also take in consideration that I use git and I've already tried to revert any changes to one of my previously stable commits. 还请考虑到我使用了git,并且我已经尝试将所有更改恢复到以前稳定的提交之一。

UPDATE: 更新:

Has anyone else encountered this problem in Android Studio? 还有其他人在Android Studio中遇到此问题吗? Is there any other solution to this kind of error that I missed or is this likely to be some bug in the latest android plugin (for android studio). 对于这种错误,我是否还有其他解决方案,或者可能是最新的android插件(适用于android studio)中的某些错误。

UPDATE (build.gradle & manifest file): UPDATE(build.gradle和清单文件):

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: 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>

I was having this same issue after updating Android Studio to 0.4.0. 将Android Studio更新到0.4.0后,我遇到了同样的问题。 It seems overly-simplistic, but I solved the issue by simply deleting my whole /build folder (the one created before the 0.4.0 update) and rebuilding. 看来过于简单了,但我通过简单地删除整个/ build文件夹(在0.4.0更新之前创建的文件夹)并重建来解决了该问题。

try this java.lang.RuntimeException: Unable to instantiate activity ComponentInfo / java.lang.ClassNotFoundException 试试这个java.lang.RuntimeException:无法实例化活动ComponentInfo / java.lang.ClassNotFoundException

or, use the CMD to access the project and Clean your project 或者,使用CMD访问项目并清理项目

gradlew clean gradlew清洁

gradlew build gradlew构建

the -2 or -1 come from having multiple copies I believe? 我相信-2或-1来自具有多个副本? At least that's what it has been in my experience. 至少这就是我的经验。 But what exactly is your question here? 但是,您到底要问什么?

The error is stating that it there are multiple copies of one of the library that you are using in your project. 该错误表明该项目中有一个库的多个副本。 Go into the project structure -> modules and check in the target or the apk-libs folder if android studio is extracting more than one copies. 进入project structure -> modules并检查targetapk-libs文件夹(如果android studio正在提取多个副本)。 I had the same error in android studio and it turned out studio was generating a new folder in the target folder called unpack which was causing the problem. 我在android studio中遇到了同样的错误,结果studio在target文件夹中生成了一个名为unpack的新文件夹,这导致了问题。

在对Android Studio进行一些更新后,该问题得以解决!

Check your project setting->modules->your project name->sources 检查您的项目设置->模块->您的项目名称->源

make sure your src and res list on right frame (named Add Content Root, res as Resources Folders, src as Sources Folders) 确保您的src和res列表在右框架(名为“添加内容根目录”,res作为“资源文件夹”,src作为“源文件夹”)

or your project encounter some errors, the stupid android studio still put the wrong apk on your device and run of course got the error. 或您的项目遇到一些错误,愚蠢的android studio仍然在您的设备上放置了错误的apk,而运行当然会出现错误。

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

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