最近在写Android的时候遇到个问题,某一Activity明明在AndroidManifest里注册过,运行时却仍无法进入该活动。
报错如下:
09-04 00:00:18.472: E/AndroidRuntime(1052): FATAL EXCEPTION: main
09-04 00:00:18.472: E/AndroidRuntime(1052): Process: com.example.compusforest, PID: 1052
09-04 00:00:18.472: E/AndroidRuntime(1052): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.compusforest/com.example.guide.GuideActivity}; have you declared this activity in your AndroidManifest.xml?
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Activity.startActivityForResult(Activity.java:3745)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Activity.startActivityForResult(Activity.java:3706)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Activity.startActivity(Activity.java:4016)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.Activity.startActivity(Activity.java:3984)
09-04 00:00:18.472: E/AndroidRuntime(1052): at com.example.compusforest.MainActivity.onClick(MainActivity.java:112)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.view.View.performClick(View.java:4780)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.view.View$PerformClick.run(View.java:19866)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.os.Handler.handleCallback(Handler.java:739)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.os.Handler.dispatchMessage(Handler.java:95)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.os.Looper.loop(Looper.java:135)
09-04 00:00:18.472: E/AndroidRuntime(1052): at android.app.ActivityThread.main(ActivityThread.java:5257)
09-04 00:00:18.472: E/AndroidRuntime(1052): at java.lang.reflect.Method.invoke(Native Method)
09-04 00:00:18.472: E/AndroidRuntime(1052): at java.lang.reflect.Method.invoke(Method.java:372)
09-04 00:00:18.472: E/AndroidRuntime(1052): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-04 00:00:18.472: E/AndroidRuntime(1052): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
报错说 have you declared this activity in your AndroidManifest.xml?
但我在AndroidManifest里写了注册活动,如下:
那么报错提示没有注册是怎么回事呢?我试了一下按住“ctrl+左键”点击注册的Activity的name名字,出现下划线超链接,点击却无法进入该活动的.java代码中。所以说明注册的确存在问题。看左边有多个package,活动的.java代码就各自放在不同的package下,所以在活动注册单写<activity android:name=".GuideActivity"></activity>是不行的。
要说清楚包名,即写作<activity android:name="com.example.guide.GuideActivity"></activity>因为省略包名则默认在AndroidManifest里规定的包内,如果活动不在此包中,按“ctrl+左键“也就无法跳转到相应活动的.java代码中,即未注册成功。
这要写清楚包名来注册就可以成功了,试一下”ctrl+左键“点击那些蓝色的Activity的name可以跳转,运行一下也成功啦,没问题啦~