internetConnectionCheck

internetConnectionCheck

这是我得到的错误

03-25 12:11:15.019  21907-21907/np.com.tester.wlms E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: np.com.tester.wlms, PID: 21907
        java.lang.RuntimeException: Unable to start activity ComponentInfo{np.com.tester.wlms/np.com.tester.wlms.MainActivity}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5137)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at np.com.tester.wlms.MainActivity.onCreate(MainActivity.java:15)
                at android.app.Activity.performCreate(Activity.java:5231)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:212)
                at android.app.ActivityThread.main(ActivityThread.java:5137)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
                at dalvik.system.NativeStart.main(Native Method)


这是我的internetConnectionCheck.java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.ActionBarActivity;


public class InternetConnectionCheck extends ActionBarActivity{
    public boolean isInternetOn()   //checking internet for connection
    {
        ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = con.getActiveNetworkInfo();
        // ARE WE CONNECTED TO THE NET
        if (networkInfo != null) {
            // MESSAGE TO SCREEN FOR TESTING (IF REQ)
            //Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
            return true;
        } else {
            return false;
        }

    }
}


这是MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
     private InternetConnectionCheck internetConnectionCheck;
     private  String internetCheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        internetCheck=String.valueOf(internetConnectionCheck.isInternetOn());

         Toast.makeText(this,internetCheck, Toast.LENGTH_SHORT);

    }



}

最佳答案

问题在这里:

 private InternetConnectionCheck internetConnectionCheck;


与这里的电话有关

internetConnectionCheck.isInternetOn()


InternetConnectionCheck是您的对象,并为其定义变量名称internetConnectionCheck。但是,您永远不会用某种东西来初始化对象。因此,它只是空的。一旦调用方法.isInternetOn(),您将始终收到NullPointerException。如果要解决此问题,则需要创建对象的实例。

private InternetConnectionCheck internetConnectionCheck = new InternetConnectionCheck();

10-04 21:07