如果我的Webview(mainWebView)当前网址包含“/ 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();
}
答案 0 :(得分:2)
我在代码中看到的一个错误是Toast.makeTest(this, "test", 2000)
您已将2000
的持续时间指定为错误。 Toast持续时间支持的唯一值是Toast.LENGTH_SHORT
和Toast.LENGTH_LONG
,其整数值为0和1.
详细了解here
答案 1 :(得分: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();
}
}
如果你得到同样的错误,那么你的问题就在其他方面了。