Discussion:
How to use onCreateOptionsMenu,onOptionsItemSelected&onPrepareOptionsMenu?
Allan Ai
2009-04-01 07:07:35 UTC
Permalink
Why I cann`t run the following case? It says there are sth. wrong at
"onCreateOptionsMenu","onOptionsItemSelected"&"onPrepareOptionsMenu" .


package com.demo.menu;

import android.app.Activity;
import android.os.Bundle;

public class MenuDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,0,0,"1");
menu.add(0,1,0,"2");
menu.add(0,2,0,"3");
menu.add(0,3,0,"4");
menu.add(0,4,0,"5");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
TextView view = (TextView) findViewById(R.id.text_view);
switch (item.getItemId()) {
case 0:
view.setText("This is"+item.getTitle());
break;
case 1:
view.setText("This is"+item.getTitle());
break;
default:
view.setText("Debug.");
break;
}
return false;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
}
Glen Humphrey
2009-04-01 23:24:36 UTC
Permalink
What does it say is wrong?
Post by Allan Ai
Why I cann`t run the following case? It says there are sth. wrong at
"onCreateOptionsMenu","onOptionsItemSelected"&"onPrepareOptionsMenu" .
package com.demo.menu;
import android.app.Activity;
import android.os.Bundle;
public class MenuDemo extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0,0,0,"1");
  menu.add(0,1,0,"2");
  menu.add(0,2,0,"3");
  menu.add(0,3,0,"4");
  menu.add(0,4,0,"5");
  return true;
 }
 public boolean onOptionsItemSelected(MenuItem item) {
  super.onOptionsItemSelected(item);
  TextView view = (TextView) findViewById(R.id.text_view);
  switch (item.getItemId()) {
   view.setText("This is"+item.getTitle());
   break;
   view.setText("This is"+item.getTitle());
   break;
            view.setText("Debug.");
  break;
  }
  return false;
 }
 public boolean onPrepareOptionsMenu(Menu menu) {
  return super.onPrepareOptionsMenu(menu);
 }
}
zhoubo shi
2009-04-02 05:00:28 UTC
Permalink
please, say error message!
Post by Glen Humphrey
What does it say is wrong?
Post by Allan Ai
Why I cann`t run the following case? It says there are sth. wrong at
"onCreateOptionsMenu","onOptionsItemSelected"&"onPrepareOptionsMenu" .
package com.demo.menu;
import android.app.Activity;
import android.os.Bundle;
public class MenuDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,0,0,"1");
menu.add(0,1,0,"2");
menu.add(0,2,0,"3");
menu.add(0,3,0,"4");
menu.add(0,4,0,"5");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
TextView view = (TextView) findViewById(R.id.text_view);
switch (item.getItemId()) {
view.setText("This is"+item.getTitle());
break;
view.setText("This is"+item.getTitle());
break;
view.setText("Debug.");
break;
}
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-beginners-***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---
Nils
2009-05-19 11:41:59 UTC
Permalink
It seems to be impossible to show checkmarks in the optionsmenu.
If that is the case, then I have to change the menu item text
dynamically to reflect the current state, right?
Comments, please!
Post by zhoubo shi
please, say error message!
Post by Glen Humphrey
What does it say is wrong?
Post by Allan Ai
Why I cann`t run the following case? It says there are sth. wrong at
"onCreateOptionsMenu","onOptionsItemSelected"&"onPrepareOptionsMenu" .
package com.demo.menu;
import android.app.Activity;
import android.os.Bundle;
public class MenuDemo extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0,0,0,"1");
  menu.add(0,1,0,"2");
  menu.add(0,2,0,"3");
  menu.add(0,3,0,"4");
  menu.add(0,4,0,"5");
  return true;
 }
 public boolean onOptionsItemSelected(MenuItem item) {
  super.onOptionsItemSelected(item);
  TextView view = (TextView) findViewById(R.id.text_view);
  switch (item.getItemId()) {
   view.setText("This is"+item.getTitle());
   break;
   view.setText("This is"+item.getTitle());
   break;
            view.setText("Debug.");
  break;
  }
  return false;
 }
 public boolean onPrepareOptionsMenu(Menu menu) {
  return super.onPrepareOptionsMenu(menu);
 }
}- Dölj citerad text -
- Visa citerad text -
Loading...