播放声音时应用程序意外停止

时间:2013-06-23 12:21:58

标签: android

  

我创建了新的 Android 应用程序,因为它显示静态信息,我创建了 html5 ,我将html5文件调用到我的webview我的一个html文件函数是播放 mp3 当点击按钮我在html和android之间做了我的界面但是当我在设备上测试项目时我有这个消息应用程序意外停止。   我有本教程中的这个项目本教程没有扩展Activity但是当我删除它时扩展了Activity 错误发生了“MediaPlayer类型中的方法create(Context,int)不适用于参数(WebAppInterface) ,int)“。

public class WebAppInterface  extends Activity    {
Context mContext;
MediaPlayer mp;


WebAppInterface(Context c) {
    mContext = c;
}


@JavascriptInterface

public void playsound() {
    mp=MediaPlayer.create(WebAppInterface.this, R.raw.sound);

}

}

 <input type="button" value="play mp3" onClick="playmp3()" />
 <script type="text/javascript">
 function playmp3()
  {
      Android.playsound();
  }
 </script>

  

06-23 15:46:20.250:E / AndroidRuntime(338):致命异常:WebViewCoreThread   06-23 15:46:20.250:E / AndroidRuntime(338):java.lang.NullPointerException   06-23 15:46:20.250:E / AndroidRuntime(338):在android.content.ContextWrapper.getResources(ContextWrapper.java:80)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.media.MediaPlayer.create(MediaPlayer.java:641)   06-23 15:46:20.250:E / AndroidRuntime(338):at com.ramadan.WebAppInterface.showToast(WebAppInterface.java:23)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.webkit.WebViewCore.nativeTouchUp(本机方法)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.webkit.WebViewCore.nativeTouchUp(本机方法)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.webkit.WebViewCore.access $ 3300(WebViewCore.java:52)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.webkit.WebViewCore $ EventHub $ 1.handleMessage(WebViewCore.java:1150)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.os.Handler.dispatchMessage(Handler.java:99)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.os.Looper.loop(Looper.java:123)   06-23 15:46:20.250:E / AndroidRuntime(338):在android.webkit.WebViewCore $ WebCoreThread.run(WebViewCore.java:621)   06-23 15:46:20.250:E / AndroidRuntime(338):at java.lang.Thread.run(Thread.java:1096)

1 个答案:

答案 0 :(得分:0)

  

问题出在这里

<code>



public void playsound() {

  MediaPlayer mp= MediaPlayer.create(mContext,R.raw.sound);
  mp.start();

}
  

当我用mContext替换WebAppInterface.this时它运行良好

相关问题