如何检查webview URL是否包含字符串?

时间:2016-06-11 01:28:13

标签: android

我正在努力让我的应用只分享一个有效的网址,否则就会显示一个祝酒词。我正在检查URL是否包含特定字符串。如果是,请分享链接,但是,我得到一个例外 -

public void sharevideo() {
    String url = webView.getUrl();
    if (url.contains("play;_ylt=")) {
        // video_is_being_played
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, url);
        startActivity(Intent.createChooser(shareIntent, "Share..."));
    } else {
        Toast.makeText(getBaseContext(), "cannot share this",
                Toast.LENGTH_LONG).show();
    }
}

包含日志 -

 06-11 06:54:08.120: E/InputEventReceiver(9116): Exception dispatching input event.
    06-11 06:54:08.120: E/MessageQueue-JNI(9116): Exception in MessageQueue callback: handleReceiveCallback
    06-11 06:54:08.122: E/MessageQueue-JNI(9116): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at com.example.myapp.MainActivity.sharevideo(MainActivity.java:446)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at com.example.myapp.MainActivity.onOptionsItemSelected(MainActivity.java:428)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.app.Activity.onMenuItemSelected(Activity.java:3694)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:185)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:655)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.AbsListView.performItemClick(AbsListView.java:1206)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3260)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.AbsListView.onTouchUp(AbsListView.java:4068)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3830)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.support.v7.widget.ListViewCompat.onTouchEvent(ListViewCompat.java:120)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.View.dispatchTouchEvent(View.java:8488)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1744)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.View.dispatchPointerEvent(View.java:8683)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3686)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3743)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.os.MessageQueue.nativePollOnce(Native Method)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.os.MessageQueue.next(MessageQueue.java:143)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:681)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.os.MessageQueue.next(<Xposed>)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.os.Looper.loop(Looper.java:122)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at android.app.ActivityThread.main(ActivityThread.java:5310)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at java.lang.reflect.Method.invoke(Native Method)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at java.lang.reflect.Method.invoke(Method.java:372)
    06-11 06:54:08.122: E/MessageQueue-JNI(9116):   at com.android.internal.os.Z
    06-11 06:54:08.123: D/AndroidRuntime(9116): Shutting down VM
    06-11 06:54:08.125: E/AndroidRuntime(9116): FATAL EXCEPTION: main
    06-11 06:54:08.125: E/AndroidRuntime(9116): Process: com.example.myapp, PID: 9116
    06-11 06:54:08.125: E/AndroidRuntime(9116): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at com.example.myapp.MainActivity.sharevideo(MainActivity.java:446)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at com.example.myapp.MainActivity.onOptionsItemSelected(MainActivity.java:428)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.app.Activity.onMenuItemSelected(Activity.java:3694)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:185)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:655)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.AbsListView.performItemClick(AbsListView.java:1206)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3260)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.AbsListView.onTouchUp(AbsListView.java:4068)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3830)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.support.v7.widget.ListViewCompat.onTouchEvent(ListViewCompat.java:120)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.View.dispatchTouchEvent(View.java:8488)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.widget.PopupWindow$PopupViewContainer.dispatchTouchEvent(PopupWindow.java:1744)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.View.dispatchPointerEvent(View.java:8683)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3686)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3743)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.os.MessageQueue.nativePollOnce(Native Method)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.os.MessageQueue.next(MessageQueue.java:143)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:681)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.os.MessageQueue.next(<Xposed>)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.os.Looper.loop(Looper.java:122)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at android.app.ActivityThread.main(ActivityThread.java:5310)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at java.lang.reflect.Method.invoke(Native Method)
    06-11 06:54:08.125: E/AndroidRuntime(9116):     at java.lang.reflect.Meth

1 个答案:

答案 0 :(得分:0)

您是否检查过您的webView对象是否已初始化或存在于该范围内?

通过Toast对象或调试消息检查您收到的URL,即getUrl()返回的内容也是明智的。

最后,请尝试使用方法.matches(String)代替.contains(CharSequence)