开发者

AlertDialog with ListView in Android

So I am starting to get into Android development with really basic applications. I'm messing around开发者_JAVA技巧 with AlertDialogs and am trying to get a list view working. I have a button that when clicked should call a function to create/display the AlertDialog. Here is the code for the button.

<Button android:layout_height="wrap_content" android:text="@string/partyChoice" android:id="@+id/partyChoiceButton" android:layout_width="wrap_content" android:layout_above="@+id/shirtSizeButton" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:onClick="partyChoice">

And here is the code for the function partyChoice

public void partyChoice()
{
    final CharSequence[] items = {"Bowling", "Laser Tag", "Combo", "Cosmic"};

    AlertDialog.Builder builder = new AlertDialog.Builder(PartyPlannerActivity.this);
    builder.setTitle("Choose A Party");
    //builder.setIcon(R.drawable.icon);
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
     Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    });

    builder.setPositiveButton("Yes",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
       Toast.makeText(PartyPlannerActivity.this, "Success", Toast.LENGTH_SHORT).show();
      }
     });
    builder.setNegativeButton("No",
     new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
       Toast.makeText(PartyPlannerActivity.this, "Fail", Toast.LENGTH_SHORT).show();
      }
     });
    AlertDialog alert = builder.create();
    alert.show();
}

This code compiles and runs with no errors, however when I go to click the button, my application force closes. I cannot find where the issue is. If anyone could help me it would be greatly appreciated.


All onClick functions which are referenced from XML have to take a View argument.

Change

public void partyChoice()

to

public void partyChoice(View v)


Check my comment above to help you pin point the exact problem. In addition, I assume your problem is the method signature. Using android:onClick calls a method that accepts the view as an argument, which means you should change the method signature to public void partyChoice(View view)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜