2011-08-31 48 views
0

所以我開始進入Android開發與真正的基本應用程序。我搞亂了AlertDialogs,並試圖讓列表視圖工作。我有一個按鈕,點擊時應該調用一個函數來創建/顯示AlertDialog。這是按鈕的代碼。AlertDialog與Android中的ListView

<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">

這裏是功能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(); 
} 

此代碼編譯並沒有錯誤運行的代碼,但是當我去按一下按鈕,我的應用程序強制關閉。我找不到問題所在。如果有人能幫助我,將不勝感激。

+1

在Eclipse中,轉到:「窗口 - >顯示視圖 - >其他」並添加Logcat視圖。這會向您顯示設備/仿真器的日誌,包括異常的堆棧跟蹤。 – IncrediApp

+0

在哪一行? ,,, – Ronnie

+0

你可以顯示來自adb logcat的堆棧跟蹤嗎? – user922295

回答

1

所有從XML引用的onClick函數都必須採用View參數。

變化

public void partyChoice() 

public void partyChoice(View v) 
+0

這工作,謝謝。不能相信我錯過了這一點。 – Cistoran

0

檢查我的上述評論,以幫助您固定點確切的問題。 另外,我假設你的問題是方法簽名。 使用android:onClick調用接受視圖作爲參數的方法,這意味着您應該將方法簽名更改爲public void partyChoice(View view)