我已經將我的應用程序簡化爲幾乎愚蠢的狀態,試圖找出問題。我正在使用AndroidStudio(IntelliJ版本)進行開發。直到最近,應用程序在啓動之前開始崩潰,我一直在開發。 Logcat顯示了這一點:應用程序被隨機轟炸,WebView拋出空指針
NullPointerException: Attempt to invoke virtual method
'void android.webkit.WebView.loadUrl(java.lang.String)'
on a null object reference
如前所述,我已經將應用程序降低到幾乎可憐的狀態。 (沒有服務,沒有接收器),這是我的活動。
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
browser = (WebView) findViewById(R.id.web_view);
browser.loadUrl("file:///android_asset/www/index.html");
}
行調用錯誤是「browser.loadUrl」文件,這已經在過去的幾個小時,工作正常。請注意,「requestWindowFeature」必須在super.onCreate之上調用,否則應用程序將崩潰。
這是我的活動(未經編輯),我的應用程序無論如何都是webview。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
我有點困惑。
你檢查瀏覽器!= null? – ByteBit
事實上,瀏覽器不應該是空的,我已經意識到瀏覽器現在返回null,並且不知道爲什麼。好吧,WebView仍然在應用程序中。 – Hobbyist