Unable to instantiate application - ClassNotFoundException on MainActivity

▼魔方 西西 提交于 2019-12-11 08:43:48

问题


I've tried cleaning my project several times, restarting Eclipse, going to Build path -> Export, making sure Android private libraries is ticked, and I've tried parsing through XML and Java files to find the error myself. I have also tried researching here on StackOverflow but to no avail.

I've also checked if I was calling things before the onCreate method.

I fear my manifest XML file might be causing an issue but I don't see anything wrong with it.

Also a little information about the project which may help you find the bug:

  1. Java files are located in the Java package com.example.myfirstapp.
  2. The "Schedule" and "MainActivity" XML files are located in layout directory.

Since the error is related to the MainActivity XML file, I'll post that and the manifest.

So here's my MainActivity XML file (file name is activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#800000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">        
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/email"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:contentDescription="@+string/logo"
        android:src="@drawable/brock_logo"/>    
    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textWebPassword"/>    
    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/password"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="textWebEmailAddress">
        <requestFocus />
    </EditText>
    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:onClick="login"
        android:text="@string/login" />    
</RelativeLayout>

Here is my AndroidManifest XML file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp"
    android:versionCode="1"
    android:versionName="1.0">    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19"/>    
    <uses-permission android:name="android.permission.INTERNET"/>    
    <application
        android:name=".MainActivity"
        android:allowBackup="true"
        android:hasCode="false"
        android:icon="@drawable/brock_logo"
        android:label="@string/app_name"
        android:logo="@drawable/brock_logo"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myfirstapp.Schedule"
            android:label="@string/app_name"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>
</manifest>

And here is my Logcat output:

09-21 13:19:41.598: D/dalvikvm(1190): Late-enabling CheckJNI
09-21 13:19:41.690: D/AndroidRuntime(1190): Shutting down VM
09-21 13:19:41.690: W/dalvikvm(1190): threadid=1: thread exiting with uncaught exception    (group=0xa4d85b20)
09-21 13:19:41.698: E/AndroidRuntime(1190): FATAL EXCEPTION: main
09-21 13:19:41.698: E/AndroidRuntime(1190): Process: com.example.myfirstapp, PID: 1190
09-21 13:19:41.698: E/AndroidRuntime(1190): java.lang.RuntimeException: Unable to instantiate application com.example.myfirstapp.MainActivity: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.LoadedApk.makeApplication(LoadedApk.java:516)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4317)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.os.Looper.loop(Looper.java:136)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.reflect.Method.invokeNative(Native Method)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.reflect.Method.invoke(Method.java:515)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at dalvik.system.NativeStart.main(Native Method)
09-21 13:19:41.698: E/AndroidRuntime(1190): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.myfirstapp.MainActivity" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib]]
09-21 13:19:41.698: E/AndroidRuntime(1190):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.Instrumentation.newApplication(Instrumentation.java:975)
09-21 13:19:41.698: E/AndroidRuntime(1190):     at android.app.LoadedApk.makeApplication(LoadedApk.java:511)
09-21 13:19:41.698: E/AndroidRuntime(1190):     ... 11 more

Sorry for dumping code and debug output. Just trying to provide as much information as possible.

Thanks for reading.


回答1:


try this:

 <application
    android:allowBackup="true"
    android:icon="@drawable/brock_logo"
    android:label="@string/app_name"
    android:logo="@drawable/brock_logo"
    android:theme="@style/AppTheme" >

then clean and rebuild your project.




回答2:


This tells Android that you want to create Application instance. You don't need that class in your app.




回答3:


I guess you actually SHOULD tick use android private libraries. also I was having same problem because of using wrong appcompat library. Make sure everything is OK with it.



来源:https://stackoverflow.com/questions/25959737/unable-to-instantiate-application-classnotfoundexception-on-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!