如果我的 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();
}