我的XML中有一个视图引用了一个方法,而该方法又应该打开AnnouncementsScreen.java类,但它会抛出以下logcat消息。它似乎声明它找不到AnnouncementsScreen.java文件,尽管它已在清单中声明,并且我已经检查了拼写。
11-09 23:05:22.040 28199-28199/? E/Zygote: MountEmulatedStorage()
11-09 23:05:22.040 28199-28199/? E/Zygote: v2
11-09 23:05:22.060 28199-28199/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: FATAL EXCEPTION: main
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: Process: com.t99sdevelopment.centralized, PID: 28199
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:259)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:300)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View.performClick(View.java:5156)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20755)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5835)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:255)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:300)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View.performClick(View.java:5156)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20755)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5835)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.t99sdevelopment.centralized.AnnouncementsScreen}; have you declared this activity in your AndroidManifest.xml?
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1499)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3913)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3860)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4184)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:4152)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.t99sdevelopment.centralized.HomeScreen.goToAnnouncements(HomeScreen.java:37)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.SupportMenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(SupportMenuInflater.java:255)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:300)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View.performClick(View.java:5156)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20755)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5835)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-09 23:05:26.090 28199-28199/com.t99sdevelopment.centralized E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
如果有人想要检查我的文件,整个GitHub项目都是here。我无法找到对我有意义的其他例外情况,所以我从这里开始,但如果有人理解其他错误并且可以解释它们,那就太棒了。
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t99sdevelopment.centralized" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AnnouncementsScreen"
android:label="@string/title_activity_announcements_screen"
android:parentActivityName=".HomeScreen"
android:theme="@style/AppTheme"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.t99sdevelopment.centralized.HomeScreen" />
</activity>
<activity android:name=".ScheduleScreen" >
</activity>
<activity android:name=".CalendarScreen" >
</activity>
<activity android:name=".ContactBookScreen" >
</activity>
<activity android:name=".SportsScheduleScreen" >
</activity>
<activity android:name=".FrontAndCentralScreen" >
</activity>
<activity android:name=".MapScreen" >
</activity>
<activity android:name=".AccountScreen" >
</activity>
</application>
</manifest>
发生错误的活动:
public class HomeScreen extends AppCompatActivity {
Intent intentAnnouncements = new Intent(this, AnnouncementsScreen.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homescreen);
setTheme(R.style.AppTheme);
}
public void goToAnnouncements(MenuItem item){ startActivity(intentAnnouncements); }
}
修改
@ njzk2指出了这个错误。在完全初始化类之前,我无法使用 this 关键字。我将意图的声明移动到它各自的方法中,现在它的工作正常。这个问题与我的Android清单无关,而是由* this(关键字。
对HomeScreen.class的引用。答案 0 :(得分:0)
你犯了一个小错误,
<activity
android:name=".AnnouncementsScreen"
android:label="@string/title_activity_announcements_screen"
android:parentActivityName=".HomeScreen"
android:theme="@style/AppTheme" >
在您的清单文件中,您已声明上述名为 .AnnouncementsScreen 的类,而您正在询问您的问题,
似乎说它无法找到 AnnouncementScreen.java 文件。
因此,两个类都不同,所以只需将您的活动名称从 .AnnouncementsScreen 替换为 .AnnouncementScreen ,它就适合您。
答案 1 :(得分:-2)
这是因为java.lang.reflect.InvocationTargetException首先验证清单文件中的目标类。
<activity
android:name=".Target"
android:label="@string/title_activity_target" >
</activity>
希望它能起作用..
答案 2 :(得分:-2)
编辑2
我已下载此代码并运行
只需更改功能goToAnnouncements:
public void goToAnnouncements(MenuItem item){
Intent intent = new Intent(this, AnnouncementsScreen.class);
startActivity(intent);
}
请更新您的代码
修改强>
您能否添加有关此类出现错误的代码?
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {/com.t99sdevelopment.centralized.AnnouncementsScreen}; have you declared this activity in your AndroidManifest.xml?
检查错误日志!请在运行app
之前将声明的所有活动添加到AndroidManifest.xml<activity
android:name="com.t99sdevelopment.centralized.AnnouncementsScreen"/>