2

这是我在这里的第一个问题,但我经常阅读这些很棒的建议。我的问题是 - 我在 WebView 中有一个包含所有本地文件的 HTML 应用程序。它工作正常。当我尝试通过 JS 接口调用服务时,就会出现问题。我的 JS 界面工作正常,并且可以使用 Toast 成功,但是当我尝试 startService(intent) 时应用程序崩溃。相同的 startService 在主 java 文件中工作正常。我认为问题在于上下文,但我不确定。显示的错误是:

04-25 21:09:55.923: W/dalvikvm(1516): threadid=9: thread exiting with uncaught exception (group=0x40015560)
04-25 21:10:20.033: E/AndroidRuntime(1516): FATAL EXCEPTION: WebViewCoreThread
04-25 21:10:20.033: E/AndroidRuntime(1516): java.lang.NullPointerException
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.content.ContextWrapper.startService(ContextWrapper.java:336)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at com.tour.giants.JavaScriptInterface.playMedia(JavaScriptInterface.java:40)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.webkit.WebViewCore.nativeTouchUp(Native Method)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.webkit.WebViewCore.nativeTouchUp(Native Method)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.webkit.WebViewCore.access$3300(WebViewCore.java:54)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1159)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.os.Looper.loop(Looper.java:123)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:630)
04-25 21:10:20.033: E/AndroidRuntime(1516):     at java.lang.Thread.run(Thread.java:1019)
04-25 21:10:20.103: I/Process(1516): Sending signal. PID: 1516 SIG: 9

这是有问题的代码:

public class JavaScriptInterface extends Activity{
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    public void playMedia(String mediaUrl) {

            //THIS WORKS!
        Toast.makeText(mContext, mediaUrl, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(mContext, MyService.class);

        // If this is AUDIO, play the audio in the background
        if(mediaUrl.endsWith(".mp3")){
            intent.putExtra("music_url", mediaUrl);
                    //THIS CRASHES!
            startService(intent);
        } 
    }
}

最后,我通过使用几乎相同的代码拦截了一个链接,成功地从 WebView 启动了这项服务。

// Create Webview client to override browser rendering url and disable address bar
private class GWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // HTML5 Audio and Video give very mixed results.  Let Android handle media player

        Intent intent = new Intent(getBaseContext(), MyService.class);

        // If this is AUDIO, play the audio in the background
        else if(url.endsWith(".mp3")){   
            intent.putExtra("music_url", url);
            startService(intent);
        } 
        // This isn't a video or audio file - load normally
        else {
            view.loadUrl(url);
        }

        return true;
    }

}

我尝试过使用不同的上下文,例如getBaseContext()等等getApplicationContext()......

4

0 回答 0