问题描述
我是新到Android
我创建了一个媒体播放器应用程序,但有一个错误不幸的是youapp停止每当我开始玩突然停止。
我应该怎么纠正我的应用程序?
请帮助我,这里的code:
包com.example.allah;
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类MainActivity延伸活动{保护无效的onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main);
最终的ListView列表视图=(ListView控件)findViewById(R.id.listView1);
的String []值=新的String [] {Allah1,Allah2};
最终的ArrayList<串GT;名单=新的ArrayList<串GT;();的for(int i = 0; I< values.length ++我){
list.add(值[I]);
}
listview.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,列表));
listview.setOnItemClickListener(新AdapterView.OnItemClickListener(){
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
myplaymethod();}
});
}
公共无效myplaymethod(){
INT的myKey = 0;
查看play_button = NULL;
开关(的myKey){
情况1:
我的MediaPlayer = MediaPlayer.create(getBaseContext(),R.raw.allah1);
my.start();
play_button.setVisibility(View.GONE);
打破;案例2:
MediaPlayer的MA = MediaPlayer.create(getBaseContext(),R.raw.allah2);
ma.start();
play_button.setVisibility(View.GONE);
打破;}
}
您MKEY值是0,并且在开关任何情况下,内搭0,像这样做
我假设你只有两首歌曲在你的列表中,因为你做只有两种情况在mplayermethod()。
在 listview.setOnItemClickListener
与mplayermethod取代你mplayermethod()(ARG2)
在mplayermethod()
公共无效myplaymethod(INT MKEY){查看play_button = NULL;
开关(的myKey){
情况下0:
我的MediaPlayer = MediaPlayer.create(getBaseContext(),R.raw.allah1);
my.start();
play_button.setVisibility(View.GONE);
打破;情况1:
MediaPlayer的MA = MediaPlayer.create(getBaseContext(),R.raw.allah2);
ma.start();
play_button.setVisibility(View.GONE);
打破;}
I am new to androidI have created a media player app but whenever i start playing it suddenly stops with an error as "Unfortunately youapp stopped".How should i rectify my app ?
Pls help me here's the code :
package com.example.allah;
import java.util.ArrayList;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listView1);
String[] values = new String[] { "Allah1", "Allah2" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
myplaymethod();
}
});
}
public void myplaymethod(){
int myKey = 0;
View play_button = null;
switch (myKey) {
case 1:
MediaPlayer my = MediaPlayer.create(getBaseContext(), R.raw.allah1);
my.start();
play_button.setVisibility(View.GONE);
break;
case 2:
MediaPlayer ma = MediaPlayer.create(getBaseContext(), R.raw.allah2);
ma.start();
play_button.setVisibility(View.GONE);
break;
}
}
Your mKey value is 0, and there is no case in switch which take 0 , Do it like this
I am supposing that you have only two songs in your list because you made only two cases in mplayermethod().
Inside listview.setOnItemClickListener
replace your mplayermethod() with mplayermethod(arg2)
and in mplayermethod()
public void myplaymethod(int mKey){
View play_button = null;
switch (myKey) {
case 0:
MediaPlayer my = MediaPlayer.create(getBaseContext(), R.raw.allah1);
my.start();
play_button.setVisibility(View.GONE);
break;
case 1:
MediaPlayer ma = MediaPlayer.create(getBaseContext(), R.raw.allah2);
ma.start();
play_button.setVisibility(View.GONE);
break;
}
这篇关于媒体播放器突然停止打球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!