Android ListActivity中的意图不起作用
我在layout.xml文件中有1个ListView和2个按钮,如下所示:Android ListActivity中的意图不起作用,android,android-layout,android-intent,android-listview,Android,Android Layout,Android Intent,Android Listview,我在layout.xml文件中有1个ListView和2个按钮,如下所示: <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:choiceMode="singleChoice"/> <LinearLayout android:layout_wi
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnBookRoomInRoomselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnBookRoomInRoomselection"
android:width="150dp" />
<Button
android:id="@+id/btnCancelInRoomselection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnCancelInRoomselection"
android:width="150dp" />
</LinearLayout>
这是我的java类:
public class RoomSelectionActivity extends ListActivity implements OnClickListener {
List<String> bookroom;
Button btnBookRoom;
Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.roomselection);
bookroom = new ArrayList<String>();
//TODO change the list content from the db
bookroom.add("Room1");
bookroom.add("Room2");
bookroom.add("Room3");
bookroom.add("Room4");
btnBookRoom = (Button)findViewById(R.id.btnBookRoomInRoomselection);
btnCancel = (Button)findViewById(R.id.btnCancelInRoomselection);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
bookroom));
btnBookRoom.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBookRoom:
Intent bookRoomIntent = new Intent(this,RoomBookedActivity.class);
startActivity(bookRoomIntent);
break;
case R.id.btnCancel:
break;
}
}
}
公共类RoomSelectionActivity扩展ListActivity实现OnClickListener{
列出书房;
按钮btnBookRoom;
按钮btnCancel;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.roomselection);
bookroom=新建ArrayList();
//TODO更改数据库中的列表内容
书房。添加(“房间1”);
书房。添加(“房间2”);
书房。添加(“3号房间”);
书房。添加(“第4房间”);
btnBookRoom=(按钮)findViewById(R.id.BTNBookroomRoomSelection);
btnCancel=(按钮)findViewById(R.id.btnCancelInRoomselection);
setListAdapter(新阵列适配器)(此,
android.R.layout.simple\u list\u item\u单选,
书房);
btnBookRoom.setOnClickListener(这个);
btnCancel.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.btnBookRoom:
Intent bookRoomIntent=新的Intent(这是RoomBookedActivity.class);
startActivity(书房意向);
打破
案例R.id.BTNCEL:
打破
}
}
}
当我单击
BookRoom
按钮时,意图不起作用。我不知道这为什么不起作用。请指导我解决此问题。案例R.id.btnbookroomroomroomselection替换
case R.id.btnBookRoom
与
哦,好吧,我错了,我有问题了。谢谢:)如果缺少开始活动的上下文,请尝试getBaseContext().startActivity(bookRoomIntent)。
case R.id.btnBookRoomInRoomselection