Android url打开期间浏览器活动中出现Null异常
我有一个只有浏览器视图的活动。每次应用程序尝试打开url时,我都会遇到以下异常:Android url打开期间浏览器活动中出现Null异常,android,google-maps,google-maps-api-2,Android,Google Maps,Google Maps Api 2,我有一个只有浏览器视图的活动。每次应用程序尝试打开url时,我都会遇到以下异常: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference 指的是这一行: mWebView.loadUrl("http://google.com"); 注
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
指的是这一行:
mWebView.loadUrl("http://google.com");
注释这一行后,“活动”将从另一个视图打开“正常”。以下是尝试加载网页的代码:
public class Browser extends AppCompatActivity {
WebView mWebView;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_browser);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.loadUrl("http://google.com");
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{karolklimek.mpkdisplay2/karolklimek.mpkdisplay2.Browser}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
at karolklimek.mpkdisplay2.Browser.onCreate(Browser.java:38)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
以及内容浏览器XML文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="karolklimek.mpkdisplay2.Browser"
>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
您在
setContentView()中有一个错误。
。您必须从R.java
文件中传递布局的整数值。因此,您必须通过R.layout.activity\u浏览器
,而不是layout.activity\u浏览器
应该是
setContentView(R.layout.activity_browser);
更新
在您的代码中,您将内容视图设置为activity\u browser
,但从您的问题中,我了解到您在content\u browser.xml
文件中有webview,因此您将获得NPE
解决此错误的最简单方法是将webview移动到您的
活动浏览器.xml
,或者将您的setContentView更改为加载内容浏览器
,而不是从代码中看到布局未加载,由于mWebView对象未初始化,因此最终将使用NPE
代码应该是
setContentView(R.layout.activity_浏览器);
相反
setContentView(layout.activity_浏览器) 您是否想到
mWebView
可能为空,并且您应该检查您的id、导入和布局?谢谢!我花了很多时间认为问题出在暂停另一项活动上,但这很简单。哈哈哈…很高兴它帮助了你…)快乐的编码!!