无法在MainActivity上实例化应用程序 - ClassNotFoundException

时间:2014-09-21 13:31:23

标签: android classnotfound

我已经多次尝试清理我的项目,重新启动Eclipse,转到Build path - >导出,确保Android私有库被勾选,我已经尝试解析XML和Java文件以自己找到错误。我也尝试过在StackOverflow上进行研究,但无济于事。

我还检查过我是否在onCreate方法之前调用了一些东西。

我担心我的清单XML文件可能会导致问题,但我认为它没有任何问题。

还有一些关于该项目的信息可以帮助您找到错误:

  1. Java文件位于Java包com.example.myfirstapp
  2. “Schedule”和“MainActivity”XML文件位于layout目录。
  3. 由于错误与MainActivity XML文件有关,我将发布该文件和清单。

    所以这是我的MainActivity XML文件(文件名是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>
    

    这是我的AndroidManifest XML文件:

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

    这是我的Logcat输出:

    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
    

    很抱歉转储代码和调试输出。只是尽量提供尽可能多的信息。

    感谢阅读。

3 个答案:

答案 0 :(得分:0)

这告诉Android您要创建Application实例。您的应用中不需要该课程。

答案 1 :(得分:0)

我猜你实际上应该勾选使用android私有库。因为使用了错误的appcompat库,我也遇到了同样的问题。确保一切正常。

答案 2 :(得分:0)

试试这个:

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

然后清理并重建您的项目。

相关问题