gpt4 book ai didi

java - 为什么在后面按这个应用程序与这个 logcat

转载 作者:行者123 更新时间:2023-12-02 09:24:24 27 4
gpt4 key购买 nike

我刚刚开始使用 android,正在制作一个 webview 应用程序

我的整个计划是显示启动屏幕,直到 webview 加载,然后切换某些内容的可见性,然后使 webview 可见

WebView 完美可见。

webview 能够加载自己点击的新网址,但一旦有人按下后退按钮,应用程序就会崩溃。这是为什么?

另一个是我想添加一个网址过滤器,它不会加载与我的网站不同的任何其他网址。我已经看到了一些关于如何做到这一点的答案,但它们也使我的应用程序崩溃。在这里我删除了该代码一次。我该如何实现这个?

这是我的主要 Activity


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
private WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.browser);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setDomStorageEnabled(true);


// also help in where to put the url filter thingy

wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// this is where i change visiblity of splash page and else
}


});
wv.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
wv.loadUrl("https://metv.ml");
}
// this is the place where i tried putting the on back press code which crashes the app on going back
}

这是我试图用来根据 webview 历史记录返回的代码

    public void onBackPressed () {
if (wv.canGoBack()) {
wv.goBack();
} else {
super.onBackPressed();
}
}

登录猫:

2019-10-18 00:48:02.916 3711-3711/com.example.metv E/InputEventSender: Exception dispatching finished signal.
2019-10-18 00:48:02.916 3711-3711/com.example.metv E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-10-18 00:48:02.917 3711-3711/com.example.metv E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.webkit.WebView.canGoBack()' on a null object reference
at com.example.metv.MainActivity.onBackPressed(MainActivity.java:33)
at android.app.Activity.onKeyUp(Activity.java:3185)
at android.view.KeyEvent.dispatch(KeyEvent.java:2743)
at android.app.Activity.dispatchKeyEvent(Activity.java:3503)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:115)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:409)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5715)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5363)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5204)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5420)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5204)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5396)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5561)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2848)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2343)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2334)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2825)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:7183)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
2019-10-18 00:48:02.917 3711-3711/com.example.metv D/AndroidRuntime: Shutting down VM
2019-10-18 00:48:02.920 3711-3711/com.example.metv E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.metv, PID: 3711
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.webkit.WebView.canGoBack()' on a null object reference
at com.example.metv.MainActivity.onBackPressed(MainActivity.java:33)
at android.app.Activity.onKeyUp(Activity.java:3185)
at android.view.KeyEvent.dispatch(KeyEvent.java:2743)
at android.app.Activity.dispatchKeyEvent(Activity.java:3503)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:115)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:409)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5715)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5363)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5204)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5420)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5204)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5177)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5237)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5196)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5396)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5561)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2848)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2343)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2334)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2825)
at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:7183)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)

最佳答案

private WebView wv;

这是一个您永远不会初始化的字段。

 public void onBackPressed () {
if (wv.canGoBack()) {

在这里,您在未初始化的字段上调用一个方法,因此是 NPE。

protected void onCreate(Bundle savedInstanceState) {
//...
WebView wv = (WebView) findViewById(R.id.browser);

这是您初始化的局部变量。将 WebView wv = 更改为 wv = 以使其初始化您的字段,而不是声明和初始化局部变量。

关于java - 为什么在后面按这个应用程序与这个 logcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58439419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com