我在应用程序中调用player.pause();
方法时收到此错误。我正在使用ImageButton暂停音乐。我正在调用song_list
类的方法来暂停轨道。但是,发生以下异常:
尝试在空对象引用上调用虚方法'void android.media.MediaPlayer.pause()'
我的主要活动:
public class MusicKom extends AppCompatActivity {
private static String TAG = "PermissionDemo";
public static final int REQUEST_READ_STORAGE = 112;
private ImageButton btplay;
private ImageButton pausemusic;
public void playbutton(int playbuttonid){
btplay=(ImageButton)findViewById(R.id.play);
btplay.setBackgroundResource(playbuttonid);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_kom);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
final viewpageadapter adapter = new viewpageadapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
Button bt1 = (Button)findViewById(R.id.song_list);
Button bt2 = (Button)findViewById(R.id.album_list);
Button bt3 = (Button)findViewById(R.id.artist_list);
pausemusic = (ImageButton)findViewById(R.id.play);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setCurrentItem(0);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setCurrentItem(1);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setCurrentItem(2);
}
});
pausemusic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
songs_list sl=new songs_list();
sl.pauseplay();
pausemusic.setBackgroundResource(R.drawable.stop);
}
});
}
}
我的song_list片段:
public class songs_list extends Fragment {
// Adapter that exposes data from a Cursor to a ListView widget.
private MediaCursorAdapter mediaAdapter = null;
public MediaPlayer player;
private String currentFile="";
private boolean isStarted = true;
private TextView selelctedFile = null;
private View v=null;
private String selectedtitem=null;
private int fileposition;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
player = new MediaPlayer();
// Use cursor to get data from external resource
final Cursor cursor = getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
// check Cursor has data or not
if (null != cursor) {
cursor.moveToFirst();
mediaAdapter = new MediaCursorAdapter(getActivity().getApplicationContext(), R.layout.list_layout, cursor);
v=inflater.inflate(R.layout.fragment_songs_list, container, false);
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setAdapter(mediaAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedtitem = (String) view.getTag();
startPlay(selectedtitem);
}
});
}
return v;
}
private void startPlay(String file) {
player.stop();
player.reset();
try {
player.setDataSource(file);
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
((MusicKom) getActivity()).playbutton(R.drawable.play);
}
public void pauseplay(){
Log.i(TAG, "pauseplay: ");
player.pause();
}
}
答案 0 :(得分:0)
如何在不实际启动活动的情况下调用其他活动方法。在您的情况下,未加载另一个活动,因此不会创建媒体播放器对象。
请发布整个代码以获取更多信息。
答案 1 :(得分:0)
您正在使用Fragment作为常规Java类。在实例化Fragment时不会调用片段onCreateView()
。只有在显示具有有效FragmentTranscation的片段时才会调用它。见Fragment Life cycle。
如果您希望在必须致电pauseplay();
时显示片段,请注释,我将添加相关代码。
否则,您应该使用常规java类从任何地方创建和播放/暂停媒体播放器。类似的东西:
// package com.example.package_name;
import android.media.MediaPlayer;
public class SoundPlayer {
private MediaPlayer mediaPlayer;
// rawSongId is in form of 'R.id.song_name'
public SoundPlayer(Context context, int rawSongId){
mediaPlayer = MediaPlayer.create(context, rawSongId);
}
public SoundPlayer(Context context, String dataSource){
// handle media player with your data source
}
public void start(){
if(mediaPlayer != null) {
mediaPlayer.start();
}
}
public void pause(){
if(mediaPlayer != null) {
mediaPlayer.pause();
}
}
public void stop(){
if(mediaPlayer != null) {
mediaPlayer.stop(); // reset(); ?
}
}
}
现在,而不是
songs_list sl = new songs_list();
sl.pauseplay();
你可以,
String dataSource = ""; // generate your dataSource here
// initialize MediaPlayer with dataSource
SoundPlayer soundPlayer = new SoundPlayer(this, dataSource);
// Use its methods
soundPlayer.start();
soundPlayer.pause();
soundPlayer.stop();
请尝试使用正确的camelCase coding conventions。您的pauseplay()
应为pausePlay();
或pauseOrPlay()
。