Marcel Drozd
Marcel Drozd

Reputation: 11

If the current browser url contains a word, do something

If my Webview (mainWebView) current URL contains a word like "/start", I want let it do something. Currently throwing out errors, some ideas?

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();
        }

}

Edit: Thanks for help, but still getting error like that one:

    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)

But only with included my If-query like above.


Still not work.

Same error, changed to:

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();
        }

Upvotes: 1

Views: 2331

Answers (2)

Eugene Pinchuk
Eugene Pinchuk

Reputation: 576

What if You try to parse "/start" in onPageFinished (or even onPageStarted)

@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();
        }
}

if You'll get the same error, so your problem in other stuff.

Upvotes: 1

AnniJais
AnniJais

Reputation: 2780

One mistake I see in your code is in the syntax of Toast.makeTest(this, "test", 2000)

You have specified the duration to 2000 which is wrong. The only values supported by Toast duration are Toast.LENGTH_SHORT and Toast.LENGTH_LONG, whose values in integer are 0 and 1.

Read more about it here

Upvotes: 2

Related Questions