Reputation: 23
Below is my Tab1.java Code. I am trying to Play a music file in this fragment.
Without implementing MediaPlayer
, app works just fine on my emulator. But when I insert the MediaPlayer
lines, even though Gradle
Builds Successfully, app doesn't open on my simulator and shows Unfortunately App stopped.
package com.example.bavarian.sos;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Tab1 extends Fragment {
ImageButton HelpButton ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* HelpButton = (ImageButton) getView().findViewById(R.id.imageButton);
HelpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer Alarm = MediaPlayer.create(getView().getContext(),R.raw.sound);
Alarm.start();
}
}); */
return inflater.inflate(R.layout.tab1, container, false);
}
}
What could be the possible reasons for this Error ?
Upvotes: 0
Views: 481
Reputation: 33
You can replace getView().getContext()
by getApplicationContext()
Upvotes: 1