This is a simple code to play a sound on click off a button
, this code was initially written in Activity but now i want to change it to Fragments.
errors
1) The method setContentView(int)
is undefined for the type Rajathmusic.
2) The method create(Context, int)
in the type MediaPlayer is not applicable for the arguments (Rajathmusic, int).
3)The method findViewById(int)
is undefined for the type Rajathmusic.
I am just starting off with android development, any help would be appreciated!
public class Rajathmusic extends Fragment {
private static final String TAG = "MyActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, "Initializing sounds...");
final MediaPlayer mp = MediaPlayer.create(this, R.raw.rajath);
Button play_button = (Button)this.findViewById(R.id.button3);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
Log.v(TAG, "Sounds initialized.");
}}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…