在java类中使用mediaPlayer并在mainActivity android上调用它

时间:2014-12-05 10:26:21

标签: java android crash android-mediaplayer

我曾尝试在类中使用MediaPlayer并在MainActivity上调用该类,但应用程序仍在崩溃。我试着搜索,但无法找到办法。任何想法?

public class TestMedia extends MainActivity {

public void Test01(){
    MediaPlayer mediaPlayer0 = MediaPlayer.create(getApplicationContext(), R.raw.one);

    mediaPlayer0.start();
}

}

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TestMedia test02 = new TestMedia();
    test02.Test01();}

1 个答案:

答案 0 :(得分:0)

提供的代码段很奇怪,为什么要实例化" TestMedia" in" onCreate()" " TestMedia"?

活动类不应该像这样实例化。

尝试替换

    TestMedia test02 = new TestMedia(); 
    test02.Test01(); 

    MediaPlayer mediaPlayer0 = MediaPlayer.create(getApplicationContext(), R.raw.one);                      
    mediaPlayer0.start();