1

如果我的 Webview (mainWebView)当前URL 包含像“/start”这样的词,我想让它做点什么。目前抛出错误,一些想法?

public class GatewayActivity extends Activity {
private String CurUrl;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gateway);

        WebView mainWebView = (WebView) findViewById(R.id.WebView1);

        // other Webview Data

        mainWebView.loadUrl("https://url.com/start");
            CurUrl = mainWebView.getOriginalUrl();

        if(CurUrl.indexOf("/start") > -1) {
            Toast error=Toast.makeText(this,  "test",  2000);   
            error.show();
        }
        else {
            Toast error=Toast.makeText(this,  "test failed",  2000);    
            error.show();
        }

}

编辑:感谢您的帮助,但仍然收到类似的错误:

    08-01 17:42:40.971: E/AndroidRuntime(16741): FATAL EXCEPTION: main
08-01 17:42:40.971: E/AndroidRuntime(16741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX.XXX/com.XXX.XXX.GatewayActivity}: java.lang.NullPointerException
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.os.Looper.loop(Looper.java:155)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread.main(ActivityThread.java:5493)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at dalvik.system.NativeStart.main(Native Method)
08-01 17:42:40.971: E/AndroidRuntime(16741): Caused by: java.lang.NullPointerException
08-01 17:42:40.971: E/AndroidRuntime(16741):    at com.XXX.XXX.GatewayActivity.onCreate(GatewayActivity.java:72)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.Activity.performCreate(Activity.java:5066)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-01 17:42:40.971: E/AndroidRuntime(16741):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)

但只有包含我的 If-query 像上面一样。


还是不行。

同样的错误,改为:

mainWebView.loadUrl("https://url.com");

        String cururl = null;
        cururl = mainWebView.getOriginalUrl();


        if(cururl.contains("/start")) {
            //Toast error=Toast.makeText(this, "test", Toast.LENGTH_SHORT); 
            //error.show();
        }
        else {
            //Toast error2=Toast.makeText(this,  "nope",  Toast.LENGTH_SHORT);  
            //error2.show();
        }
4

2 回答 2

2

我在您的代码中看到的一个错误是Toast.makeTest(this, "test", 2000)

您已指定错误的持续时间2000。Toast 持续时间支持的唯一值是Toast.LENGTH_SHORTToast.LENGTH_LONG,其整数值是 0 和 1。

在此处阅读有关它的更多信息

于 2013-08-01T14:18:02.207 回答
1

如果您尝试在 onPageFinished(甚至 onPageStarted)中解析“/start”怎么办?

@Override
public void onPageFinished(WebView view, String url) { 
 super.onPageFinished(view, url);
 if(url.contains("/start")) {
            Toast.makeText(GatewayActivity.this, "test", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(GatewayActivity.this, "test", Toast.LENGTH_LONG).show();
        }
}

如果你会得到同样的错误,那么你在其他方面的问题。

于 2013-08-02T14:13:54.010 回答