大家好我正在尝试创建菜单和子菜单。菜单部分工作正常,但每次长按调用子菜单的文本,应用程序崩溃。请问我做错了什么? 这是我的代码:
package com.santia.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
public class BuildingMenus extends Activity {
private final int menuAdd = 1, menuSend = 2, menuDel = 3;
private final int groupDefault = 0, groupDel= 1;
private final int idDefault = 0;
private final int idText1= 1, idText2 = 2, idText3 = 3;
private String[] Choices={"Press Me", "Try Again", "Change Me"};
private static int itemNum=0;
private static TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.menu.bm);
txt = (TextView) findViewById(R.id.txtT);
registerForContextMenu((View) findViewById(R.id.txtT));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(groupDefault, menuAdd, 0, "Add").setIcon(R.drawable.ic_launcher);
menu.add(groupDefault, menuSend, 0, "Send");
menu.add(groupDel, menuDel, 0, "Delete");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
if(itemNum>0){
menu.setGroupVisible(groupDel, true);
}else {
menu.setGroupVisible(groupDel, false);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case menuAdd:
create_note();
return true;
case menuSend:
send_note();
return true;
case menuDel:
delete_note();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
if (v.getId()==R.id.txtT){
SubMenu textmenu = menu.addSubMenu("Change Text");
textmenu.add(0, idText1, 0, Choices[0]);
textmenu.add(0, idText2, 0, Choices[1]);
textmenu.add(0, idText3, 0, Choices[3]);
textmenu.add(0, idDefault, 0, "Original Text");
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case idDefault:
txt.setText(R.string.hello_world);
return true;
case idText1:
case idText2:
case idText3:
txt.setText(Choices[item.getItemId()-1]);
}
return super.onContextItemSelected(item);
}
private void delete_note() {
// TODO Auto-generated method stub
itemNum--;
}
private void send_note() {
// TODO Auto-generated method stub
}
private void create_note() {
// TODO Auto-generated method stub
itemNum++;
}
}
这是logcat的错误
07-22 21:13:18.395: E/AndroidRuntime(665): FATAL EXCEPTION: main
07-22 21:13:18.395: E/AndroidRuntime(665): java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
07-22 21:13:18.395: E/AndroidRuntime(665): at com.santia.myfirstapp.BuildingMenus.onCreateContextMenu(BuildingMenus.java:72)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.View.createContextMenu(View.java:6121)
07-22 21:13:18.395: E/AndroidRuntime(665): at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
07-22 21:13:18.395: E/AndroidRuntime(665): at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2124)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:566)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:566)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:566)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.View.showContextMenu(View.java:3532)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.View.performLongClick(View.java:3501)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.widget.TextView.performLongClick(TextView.java:9228)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.view.View$CheckForLongPress.run(View.java:13961)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.os.Handler.handleCallback(Handler.java:605)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.os.Looper.loop(Looper.java:137)
07-22 21:13:18.395: E/AndroidRuntime(665): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-22 21:13:18.395: E/AndroidRuntime(665): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 21:13:18.395: E/AndroidRuntime(665): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 21:13:18.395: E/AndroidRuntime(665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-22 21:13:18.395: E/AndroidRuntime(665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-22 21:13:18.395: E/AndroidRuntime(665): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Choices [3]不是该数组的有效索引。使用选择[2]。