1
我想播放聲音,當按鈕單擊時,我有不同的聲音,我列出的聲音與列表視圖,singlelistitem打開佈局時,我在列表視圖上選擇項目,我單擊單一列表佈局的按鈕,但當我點擊應用程序被關閉,它不工作,問題出在哪裏,謝謝MediaViewer的問題在列表視圖
AndroidListViewActivity.class(用於列表,選擇項)
SingleListItem.class(顯示所選項目)
按鈕點擊(在SingleListItem佈局上播放選定的聲音)
logcat的消息:
11-11 16:18:09.285: E/dalvikvm(17161): VM aborting
11-11 16:18:09.285: A/libc(17161): Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)
AndroidListViewActivity類;
public class AndroidListViewActivity extends ListActivity {
int [] sounds;
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sounds= new int[] {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};
// storing string resources into Array
String[] sounds= getResources().getStringArray(R.array.sounds);
// Binding Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, sounds));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String selectedsound= ((TextView) view).getText().toString();
String selectedFromList = getItemAtPosition(position);
@SuppressWarnings("unused")
int resId = getResources().getIdentifier(selectedFromList, "raw", getPackageName());
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("selectedsound", selectedsound);
intent.putExtra("position", position);
startActivity(i);
startActivity(intent);
}
private String getItemAtPosition(int position) {
// TODO Auto-generated method stub
return null;
}
});
}
}
SingleListItem class;
public class SingleListItem extends Activity{
int [] sounds;
MediaPlayer mediaPlayer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sounds= new int[] {R.raw.sound1,R.raw.sound2,R.raw.sound3,R.raw.sound4,R.raw.sound5};
this.setContentView(R.layout.single_list_item_view);
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
Intent intent = getIntent();
// getting attached intent data
String product = i.getStringExtra("selectedsound");
int position = intent.getExtras().getInt("position");
// displaying selected sound
txtProduct.setText(selectedsound);
mediaPlayer = MediaPlayer.create(this, sounds[position]);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0) {
mediaPlayer.start();
//start the progress dialog
}
});
}
}