0

So I am starting to get into Android development with really basic applications. I'm messing around 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.

3
  • 1
    In Eclipse, go to: "Window -> Show View -> Other" and add the Logcat view. This will show you the logs of your device / emulator including the stack trace of the exception. Commented Aug 31, 2011 at 19:16
  • Can you show the stack trace from adb logcat? Commented Aug 31, 2011 at 19:17
  • Yeah, the logcat view is not obvious, good thing I started Android with an experienced mentor or I would have looked for a while... Commented Aug 31, 2011 at 19:21

2 Answers 2

1

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

Change

public void partyChoice()

to

public void partyChoice(View v)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.