gpt4 book ai didi

java - 应用程序因空指针异常而崩溃,并且在 android webview 中按下“返回”时出现其他错误

转载 作者:行者123 更新时间:2023-12-01 22:23:10 24 4
gpt4 key购买 nike

//Logcat错误

2019-10-27 08:20:27.484 11011-11011/com.patherpane.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.patherpane.app, PID: 11011
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.webkit.WebView.canGoBack()' on a null object reference
at com.patherpane.app.MainActivity.onBackPressed(MainActivity.java:28)
at android.app.Activity.onKeyUp(Activity.java:3082)
at android.view.KeyEvent.dispatch(KeyEvent.java:2716)
at android.app.Activity.dispatchKeyEvent(Activity.java:3365)
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:342)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5037)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4905)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4585)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4642)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4453)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4426)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4479)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4445)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4618)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4779)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2571)
at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2081)
at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2072)
at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2548)
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:160)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-10-27 08:20:28.410 1859-1935/? E/InputDispatcher: channel 'c6baa63 com.patherpane.app/com.patherpane.app.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-27 08:20:28.552 1596-1596/? E/lowmemorykiller: Error opening /proc/11060/oom_score_adj; errno=2
2019-10-27 08:20:28.930 1596-1596/? E/lowmemorykiller: Error opening /proc/11011/oom_score_adj; errno=2
2019-10-27 08:20:29.089 1596-1596/? E/lowmemorykiller: Error opening /proc/11060/oom_score_adj; errno=2
2019-10-27 08:20:29.952 1596-1596/? E/lowmemorykiller: Error opening /proc/11060/oom_score_adj; errno=2
2019-10-27 08:20:30.244 1859-1876/? E/libprocessgroup: Error encountered killing process cgroup uid 99032 pid 11060: No such file or directory
2019-10-27 08:20:36.978 1859-1872/? E/memtrack: Couldn't load memtrack module
2019-10-27 08:20:41.818 2292-2292/? E/BeaconBle: Missing BluetoothAdapter
2019-10-27 08:20:41.913 2292-2292/? E/BeaconBle: Scan couldn't start for Places
2019-10-27 08:20:43.284 1735-1735/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2019-10-27 08:20:43.284 1735-1735/? E/netmgr: WifiForwarder unable to open QEMU pipe: Invalid argument
2019-10-27 08:20:53.224 1679-1755/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0

//MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.patherpane.com");
myWebView.setWebViewClient(new WebViewClient());}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}


//activity_main.xml


<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="fill"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


//Manifest File


<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen"
android:theme="@style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:theme="@style/AppTheme"/>
</application>
</manifest>

// list 中授予的互联网权限

最佳答案

您已在 onCreate() 中定义了 myWebView

WebView myWebView = findViewById(R.id.webView);

但是您还有一个全局 WebView myWebView 变量,该变量未初始化,因此为 null。

转换成这个

setContentView(R.layout.activity_main);
myWebView = findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();

关于java - 应用程序因空指针异常而崩溃,并且在 android webview 中按下“返回”时出现其他错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576352/

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