我嘗試了多次清理我的項目,重新啓動Eclipse,轉到構建路徑 - >導出,確保Android私有庫被打勾,並且我嘗試了通過XML和Java進行解析文件來自己查找錯誤。我也試圖在這裏研究StackOverflow,但無濟於事。無法實例化應用程序 - MainActivity上的ClassNotFoundException
我也檢查過我是否在onCreate
方法之前調用過東西。
我擔心我的清單XML文件可能導致問題,但我沒有看到任何問題。
而且對項目可能幫助您找到錯誤的少量信息:
- Java文件都位於Java包
com.example.myfirstapp
。 - 「Schedule」和「MainActivity」XML文件位於
layout
目錄中。
由於錯誤與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
對轉儲代碼和調試輸出抱歉。只是儘量提供儘可能多的信息。
感謝您的閱讀。
您已經完全聲明瞭該包的包,而不是MainActivity。你確定主要活動在那個包裏嗎? – mobilepotato7 2014-09-21 14:50:48