I am trying to start an activity from an options menu, but my app keeps crashing. The only error that I receive is an ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord,Intent) error in the debug window in Eclipse.
Below is the the code that I am using at the moment, but keep in mind I have tried multiple options, all of which end in the same misery, at the same piece of code - the startActivity statement (discovered by using breakpoints, since I'm not sure how to see the stack trace in the LogCat window, as described in my previous question Android/Eclipse: assistance with LogCat).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.changescheme, menu);
menu.findItem(R.id.changeScheme).setIntent(new Intent(this, ColourActivity.class));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
startActivity(item.getIntent());
return true;
}
And here is the changescheme.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/changeScheme" android:title="Change Colour Scheme" android:icon="#android:drawable/ic_menu_edit"></item>
</menu>
I have also tried using a switch(item.getItemId()) statement in the onOptionsItemSelected block as opposed to the menu.findItem in the onCreateOptionsMenu block, but still no luck.
I have defined the activity in my Manifest file. I can also start the activity from a regular button, and the first time the app opens on a device, the activity is started immediately after my splash screen, and I have had no problems with either of these methods.
To me this indicates that there is nothing wrong with the ColourActivity class or its associated layout file, but there is a problem with the implementation from the options menu.
I have also implemented this same method as shown above (in code) in a different app and had no problems, so I'm am really at a loss here.
Intent you are activating should point to some target component, which is not in your case, instead you should do following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
this.closeOptionsMenu();
Intent intent = new Intent(ActivityA.this, ColourActivity.class);
/*Here ActivityA is current Activity and ColourActivity is the target Activity.*/
startActivity(intent);
return true;
}
Try with this,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflator = getMenuInflater();
inflator.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.changeScheme:
Log.d("ChangeScheme", "Selected : ChangeScheme Option");
startActivity(new Intent(MainAcitivity.this, ColourActivity.class));
return true;
caseR.id.help:
Log.d("HelpMenu", "Selected : Help Option");
//Here put your code
return true;
}
}
Check this:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.changescheme, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.changeScheme:
//start activity here
break;
case R.id.help:
//start activity here
break;
}
return true;
}
Hi adam your code seem to be perfectly fine while i am testing on my emulator, please check whether you have added the class name "ColourActivity" to your manifest file.
<activity android:name="ColourActivity"></activity>
I have solved the problem now.
It turns out the problem was not at all on the ListActivity class, it was in fact on the ColourActivity class.
I was attempting to parse a few colours in onCreate, but I had forgotten to include the # in one of the RGB colour strings, hence the crash!
Thanks heaps for everyone's help, Adam.
Related
on android phones there is a button at the bottom left. this usually opens up a menu. what is the code for this. Its okay to lead me to another post instead of posting a long answer here. sorry Im asking. I understand if there are other posts like this one. I just couldnt seem to find one. ill delete this one when done. Thanks.
Check out the developer guide on menu :) http://developer.android.com/guide/topics/ui/menus.html
To specify the options menu for an activity, override onCreateOptionsMenu() (fragments provide their own onCreateOptionsMenu() callback). In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
To handle click events:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I have the problem that my application doesn't show the options menu when I hit the menu button. Debugging shows that the onCreateOptionsMenu(Menu menu) method is not called after hitting the menu button. I have another application with the same code for the menu and there it works. So now my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.app_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.options:
Intent intent = new Intent(this, OptionsActivity.class);
startActivityForResult(intent, 1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
In res -> menu -> app_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/options" android:title="#string/options" />
</menu>
I have no idea why the onCreateOptionsMenu is not called after hitting the menu button. I hope you guys can help me.
Edit: I'm not using Fragments and the onCreateOptionsMenu is really never called. Not at the start of the app and not when i'm hitting the menu button on my device.
Not sure from your post that you're using Fragments. If so, you must set menu options on by
setHasOptionMenu(true);
call this method from Fragment's onCreate() and the options menu will then be shown.
Try to add these item attributes in your app_menu.xml file:
<item
android:id="#+id/options"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/options"/>
You must declare a string for options in Strings.xml
It will call when app starts for the first time not after menu item selected.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (item.getItemId() == R.id.menuitem_id) {
}
return super.onMenuItemSelected(featureId, item);
}
this method will be called after selection
In Manifest file just set target versions as below:
android:minSdkVersion="8" android:targetSdkVersion="10"
I have the below code and I can't understand why my code doesn't show up menu. If the activity containing data, condition_true = true otherwise condition_true = false. I can clearly see the control going to menutag and the condition is true too. Even I am getting Inflated string too in log. But still menu doesn't show up. I have seen lot of posts on these like link 1, link2 etc., but that didn't solve my problem. Can someone please help me on this issue?
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getSupportMenuInflater();
Log.d("menutag", condition_true);
if(condition_true)
{
inflater.inflate(R.layout.menu, menu);
Log.d("menutag","Inflated");
}
return super.onPrepareOptionsMenu(menu); // replaced this with true, but no use.
}
You have to add the options attributes in xml.. Otherwise you can do it programmatically like this:
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.yourmenuxmlfilename, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId){
case R.id.item1:
// what you want to do with first button
break;
case .....
break;
}
return true;
}
Maybe you're overriding onKeyDown and it always returns true? This can cause the problem, because it will catch the menu button pressed event too.
I am trying to use the onPrepareOptionsMenu and when I press the menu, it does nothing!! Where am I going wrong?
Also, am I using the finish (); command right? When the user presses exit(menuX) I want them to exit the application completely, not the activity.
Thanks! :)
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater getItnow = getMenuInflater();
getItnow.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onPrepareOptionsMenu(MenuItem item){
switch (item.getItemId()){
case R.id.menuA:
startActivity(new Intent("com.abc.example.A"));
return true;
case R.id.menuB:
startActivity(new Intent("com.abc.example.B"));
return true;
case R.id.menuC:
startActivity(new Intent("com.abc.example.C"));
return true;
case R.id.menuD:
startActivity(new Intent("com.abc.example.D"));
case R.id.menuX:
finish();
}
return false;
}
}
First off all I would rather use onOptionsItemSelected then onPrepareOptionsMenu. Because as I understand you just want to press it. It is quite the same only first line is changed.
The second thing - app exit. Well, if this menu is in your main and first activity, then when you call finish() then this activity is destroyed (not exactly but the thing is that you don't see it anymore), so if app has no more activity everything is destroyed.
More advanced method for that is using instruction android.os.Process.killProcess(android.os.Process.myPid()); and what it is doing is simply sending kill signal for whole process. As far as your app won`t get bigger, I mean you will have only your activities in this process, than it is fine. Hope it will help.
When I create an options menu, I usually use these two methods:
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1, 0, "option text");
.....
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == 1)
...
}
Also, with your switch statement, you are forgetting to put in break; for each option (which I notice mostly because I am notorious for doing the same).
In my main activity I have a menu and when a menu option is selected an Intent is created and a new activity is started. When that activity completes the process should return back to the main activity and all its previous states according to the ActivityLifeCycle.
I notice that when it returns back to the main activity, nothing is accessable and the screen dims. I can only get back to what I expect when I press the menu softkey.
Has anyone experienced this issue before? Feedback would be appreciated!
Code sample below:
#Override
protected void onResume(){
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
case R.id.menu_decks:
Intent launchDecks = new Intent(this, stackDecks.class);
startActivity(launchDecks);
return true;
case R.id.menu_exit:
this.onDestroy();
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The code in the first post is actually correct(I have something very similar). There is a great chance the error is in the menu.xml res file, even though it inflates without any problems. Check it is fully consistent in both places, and has the same items. I finally solved it after hours of experimenting.
You seemed to be confused with the use of super.
super.m() is used to call a superclass method.
If you inherit the method with no override then
super.m() = this.m()
See your super.onDestroy
It s absoluetly useless to override a method m() just to call super.m()
See your onResume
Sometimes it is usefull to call a super clas method, it allows you to benefit from this code in a subclass. For instance here onCreateMenuOptions is overriden and your subclass can benefit from some imitialisation code for a menu.
Regards,
Stéphane