无法在空对象引用上启动活动(...)

时间:2017-07-12 13:21:16

标签: java android nullreferenceexception

我无法完成代码。一切都有效,除了一件事。我一直得到一个null错误,它引用了无法调用的webViewClient。该应用程序工作正常,除非我关闭WiFi和移动网络。它应该显示一个对话框,但应用程序停止。我已经有了它的工作,但显然我已经改变了一些坏事。这是我的错误:

07-12 12:59:33.770 21023-21023/companyname.appname
E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: companyname.appname, PID: 21023
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{companyname.appname/companyname.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setWebViewClient(android.webkit.WebViewClient)' on a null object reference
                                                                                 at companyname.appname.MainActivity.onCreate(MainActivity.java:49)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我的代码的一部分:

public class MainActivity extends AppCompatActivity {

WebView browser;
FloatingActionButton fab_plus,fab_settings,fab_share, fab_report;
Animation FabOpen,FabClose,FabRClockwise,FabRantiClockwise;
boolean isOpen = false;
boolean doubleBackToExitPressedOnce = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(!isConnected(MainActivity.this)) buildDialog(MainActivity.this).show();
    else {
        setContentView(R.layout.activity_main);
    }
    browser=(WebView) findViewById(R.id.webView);
    browser.setWebViewClient(new WebViewClient() {
                                 @Override
                                 public boolean shouldOverrideUrlLoading(WebView view,  String url) {
                                     view.loadUrl(url);
                                     return (true);
                                 }
                             });
    String url = "here's my url";
    WebView view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    CookieManager.getInstance().setAcceptCookie(true);
    view.loadUrl(url);


//Over here there's a lot of code which I think is irrelevant for this problem. And then at the bottom there's my dialog and internet check:


public boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();
    if (netinfo != null && netinfo.isConnectedOrConnecting()) {
        NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
    else return false;
    } else
    return false;
}
public AlertDialog.Builder buildDialog(Context c) {
    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("No connection");
    builder.setMessage("Text");
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    return builder;
}
}

此处是activity_main XMl文件中Webview的一部分:

    <WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

</WebView>

我觉得这很简单,我无法看到。你们有什么想法吗?非常感谢提前!

3 个答案:

答案 0 :(得分:0)

摆脱这条线:

browser.setWebViewClient(new WebViewClient());

首先,在为browser分配值之前,您正在调用它。这会导致你的崩溃。

其次,你以后第二次打电话给setWebViewClient(),这使得这条线毫无意义。

答案 1 :(得分:0)

而不是

  browser.setWebViewClient(new WebViewClient());
  browser=(WebView) findViewById(R.id.webView);

写这个

  browser=(WebView) findViewById(R.id.webView);    //first get reference
  browser.setWebViewClient(new WebViewClient());   //then call method

答案 2 :(得分:0)

问题是您在实例化webview之前调用此方法

browser.setWebViewClient(new WebViewClient());  <-- remove this line 
//Zorgt ervoor dat de browser de app niet verlaat
browser=(WebView) findViewById(R.id.webView);
browser.setWebViewClient(new WebViewClient() 

应该做的伎俩