boolean android.net.NetworkInfo.isConnectedOrConnecting()在Android 5.1.1中不起作用

时间:2015-07-27 13:52:03

标签: android android-5.1.1-lollipop

我正在开发Android平板电脑应用程序,我正在检查名为“Home”的Activity中的Internet连接。我已经在Android版本4.2(平板电脑),4.4(平板电脑),5.0(手机),5.0.2(手机)中测试了应用程序,该应用程序运行正常。

问题: 当我在Android 5.1.1(平板电脑)中测试应用程序时,应用程序被强制关闭。它在日志中显示以下错误。

日志:

2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freestyle/com.freestyle.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.access$800(ActivityThread.java:151)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.os.Handler.dispatchMessage(Handler.java:102)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.os.Looper.loop(Looper.java:135)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.main(ActivityThread.java:5254)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at java.lang.reflect.Method.invoke(Native Method)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at java.lang.reflect.Method.invoke(Method.java:372)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isConnectedOrConnecting()' on a null object reference
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.freestyle.utils.NetworkHelper.isConnectingToInternet(NetworkHelper.java:20)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at com.freestyle.Home.onCreate(Home.java:332)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.Activity.performCreate(Activity.java:5990)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
2015-07-27 05:36:19.312 ERROR:  AndroidRuntime : ... 10 more
2015-07-27 05:36:19.313 WARN:   ActivityManager : Force finishing activity 1 com.freestyle/.Home
2015-07-27 05:36:19.367 INFO:   OpenGLRenderer : Initialized EGL, version 1.4
2015-07-27 05:36:19.369 DEBUG:  mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.383 DEBUG:  mali_winsys : new_window_surface returns 0x3000
2015-07-27 05:36:19.817 WARN:   ActivityManager : Activity pause timeout for ActivityRecord{2c5a4722 u0 com.freestyle/.Home t22999 f}

检查互联网连接的方法:

public boolean isConnectingToInternet() {
        boolean status = false;

            ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()
                    && cm.getActiveNetworkInfo().isAvailable()
                    && cm.getActiveNetworkInfo().isConnected()) {
                //have to assign true
                status = true;
                return status;
            }

        return status;
    }

家庭活动代码:

public class Home extends Activity implements OnClickListener {

    NetworkHelper nh = null;

@Override
    protected void onCreate(Bundle savedInstanceState) {

    nh = new NetworkHelper(HomeActivity.this);

    if (nh.isConnectingToInternet()) {
            ApplicationUpdates app = new ApplicationUpdates(HomeActivity.this,
                    loadingString, updateMessage);
            app.fetchUpdate();
        }
    }
}

5 个答案:

答案 0 :(得分:5)

检查这种方式。它对mw的工作很好

public static boolean checkInternetConnection(Context context)
    {
        try
        {
            ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() && conMgr.getActiveNetworkInfo().isConnected())
                return true;
            else
                return false;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return false;
    }

答案 1 :(得分:0)

你犯了逻辑错误。其实你的代码是真的。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
 if (textField == self.txname)
 {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
    return NO;
}

NSUInteger newLength = [textField.text length] + [string length] - range.length;
return newLength <= 25;
}
 return YES;
}

答案 2 :(得分:0)

最后我使用了以下方法

public static boolean isNetworkConnected(Context c) {
    ConnectivityManager connectivityManager =
        (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

答案 3 :(得分:0)

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null) {
    Toast.makeText(RefreshFunctionsActivity.this, "No save wifi detected.", Toast.LENGTH_LONG).show();
    return;
}

答案 4 :(得分:0)

Kotlin 扩展函数 :-

$transactions->count() > 0