各位大侠求助啊,安卓程序不能运行,总是意外终止
public class ExampleActivity extends Activity {
TextView tv=null;
EditText et=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.textview);
et=(EditText)findViewById(R.id.edittext);
this.registerForContextMenu(tv);
this.registerForContextMenu(et);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo){
menu.setHeaderIcon(R.drawable.ic_launcher);
if(v==tv){
menu.add(0,1,1,"10号");
menu.add(0,2,2,"20号");
menu.add(0,3,3,"30号");
menu.add(0,4,4,"40号");
}else if(v==et){
menu.add(0,5,5,"50号");
menu.add(0,6,6,"60号");
}
}
}
[解决办法]
应该是
if(v.getId()==tv){ menu.add(0,1,1,"10号"); menu.add(0,2,2,"20号"); menu.add(0,3,3,"30号"); menu.add(0,4,4,"40号"); }else if(v.getId()==et){ menu.add(0,5,5,"50号"); menu.add(0,6,6,"60号"); } }