简体   繁体   中英

Android - Unable to instantiate activity

I'm currently using Android Studio to develop my own project for Android (my custom launcher)! The thing is, since last update (to version 0.4.0) I can't run my app.

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)
  • clean/build (error persists)
  • check the "AndroidManifest.xml" (everything seems correct)
  • 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.

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.

UPDATE:

Has anyone else encountered this problem in 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).

UPDATE (build.gradle & manifest file):

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>

I was having this same issue after updating Android Studio to 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.

try this java.lang.RuntimeException: Unable to instantiate activity ComponentInfo / java.lang.ClassNotFoundException

or, use the CMD to access the project and Clean your project

gradlew clean

gradlew build

the -2 or -1 come from having multiple copies I believe? 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. 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进行一些更新后,该问题得以解决!

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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