Solution: So, after being stuck on this for quite a while, I finally discovered that the issue was because my activity was titled Menu. I guess this is a reserved word in some way or another and because of that, it would cause random crashes to occur when trying to start the intent Menu::class.java
. I changed the name of the activity and everything works as expected now.
I have added an Options Menu to an app that I am working on which contains a "home" button. The button works most of the time but periodically it causes the app to crash when it’s clicked. It seems to happen after the button has already been clicked at least once and/or after the native back button has been pressed. Additionally, once the button is pressed, it seems to close the last activity. Clicking the back button after clicking the home button does not take me back to where I was. Below is my code for the toolbar.
// Creates toolbar
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.home, menu)
return true
}
// Listens to click of toolbar
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == R.id.home) {
val intent = Intent(this, Menu::class.java)
startActivity(intent)
}
return super.onOptionsItemSelected(item)
}
Here’s the logcat:
2023-11-28 11:58:15.760 6693-6693 AndroidRuntime
com.benwhittington.flanges D Shutting down VM 2023-11-28
11:58:15.765 6693-6693 AndroidRuntime
com.benwhittington.flanges E FATAL EXCEPTION: main
Process: com.benwhittington.flanges, PID: 6693
android.content.ActivityNotFoundException: Unable to find explicit
activity class {com.benwhittington.flanges/android.view.Menu}; have
you declared this activity in your AndroidManifest.xml, or does your
intent not match its declared ?
at
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2171)
at
android.app.Instrumentation.execStartActivity(Instrumentation.java:1805)
at android.app.Activity.startActivityForResult(Activity.java:5596)
at
androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:780)
at android.app.Activity.startActivityForResult(Activity.java:5554)
at
androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:761)
at android.app.Activity.startActivity(Activity.java:6052)
at android.app.Activity.startActivity(Activity.java:6019)
at
com.benwhittington.flanges.FlangeInspection.onOptionsItemSelected(FlangeInspection.kt:242)
at android.app.Activity.onMenuItemSelected(Activity.java:4532)
at
androidx.activity.ComponentActivity.onMenuItemSelected(ComponentActivity.java:528)
at
androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
at
androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:269)
at
androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:110)
at
androidx.appcompat.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:66)
at
androidx.appcompat.widget.Toolbar$1.onMenuItemClick(Toolbar.java:225)
at
androidx.appcompat.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:781)
at
androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:836)
at
androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:159)
at
androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:987)
at
androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:977)
at
androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
at
androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:156)
at android.view.View.performClick(View.java:7892)
at android.widget.TextView.performClick(TextView.java:16220)
at android.view.View.performClickInternal(View.java:7869)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:30891)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Here’s my manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Flanges"
tools:targetApi="31">
<activity
android:name=".ViewISO"
android:exported="false" />
<activity
android:name=".ViewInspection"
android:exported="false" />
<activity
android:name=".AddEmail"
android:exported="false" />
<activity
android:name=".FlangeInspection"
android:exported="false" />
<activity
android:name=".SignUp"
android:exported="false" />
<activity
android:name=".Menu"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
2
Answers
So, after being stuck on this for quite a while, I finally discovered that the issue was because my activity was titled Menu. I guess this is a reserved word in some way or another and because of that, it would cause random crashes to occur when trying to start the intent Menu::class.java. I changed the name of the activity and everything works as expected now.
I believe this error occurs because even though you have Menu activity declared in your Android Manifest, it seems you have Menu Activity nested in folders, so, you have to add to your Android Manifest the entire route (I belive is android.view.Menu)
Your code:
New code: