列表视图上的Mediaplayer问题

时间:2012-11-13 08:54:13

标签: android android-listview android-mediaplayer

我想在点击按钮时播放声音,我有不同的声音,我在listview中列出声音,当我在listview上选择项目时,singlelistitem打开布局,我单击singlelistitem布局上的按钮,但应用程序正在关闭时我点击了,它不起作用,问题出在哪里,谢谢

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类;

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
     }
    }); 
 }    
}

1 个答案:

答案 0 :(得分:0)

删除resId(您尚未使用它),因为您将Null传递给它:

String selectedFromList = getItemAtPosition(position);

int resId = getResources().getIdentifier(selectedFromList, "raw", getPackageName());

private String getItemAtPosition(int position) {
    // TODO Auto-generated method stub
    return null;
}