我试图检查internetConnection时收到java.lang.NullPointerException?

时间:2015-03-25 06:39:08

标签: java android android-studio

这是我得到的错误

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);

    }



}

4 个答案:

答案 0 :(得分:1)

您必须创建InternetConnectionCheck的实例。

否则isInternetOn必须是静态方法

public statis boolean isInternetOn()
{

}

答案 1 :(得分:1)

public class Utility {
public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}


//Use it in your activity
boolean connected = Utility.isNetworkAvailable(this);

答案 2 :(得分:0)

问题是InternetConnectionCheck没有初始化为什么你得到null异常。

试试这个

<强> 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 static  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);

    }



}

答案 3 :(得分:0)

问题在于:

 private InternetConnectionCheck internetConnectionCheck;

与此处的电话相关

internetConnectionCheck.isInternetOn()

InternetConnectionCheck是您的对象,您可以为其定义变量名internetConnectionCheck。但是你从来没有用某些东西初始化对象。因此它只是空。只要您调用方法.isInternetOn(),您就会收到NullPointerException。如果要解决此问题,则需要创建对象的实例。

private InternetConnectionCheck internetConnectionCheck = new InternetConnectionCheck();