【问题标题】:Intent in ListActivity not workingListActivity 中的意图不起作用
【发布时间】:2012-04-13 07:18:38
【问题描述】:

我在 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_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;
    }

}

}

当我单击BookRoom 按钮时,意图不起作用。我不确定为什么这不起作用。请指导我解决这个问题。

【问题讨论】:

  • 哦,好吧,我的错误,我遇到了问题。谢谢:)
  • 您缺少启动活动的上下文,请尝试 getBaseContext().startActivity(bookRoomIntent); .

标签: android android-layout android-intent android-listview


【解决方案1】:

替换

case R.id.btnBookRoom

case R.id.btnBookRoomInRoomselection

【讨论】:

    【解决方案2】:

    案例 R.id.btnBookRoomInRoomselection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      相关资源
      最近更新 更多