我知道这里已经问过这个话题,但我不明白为什么我的代码没有按预期工作。我需要在启动我的应用程序时检查数据连接,但它在某些平板电脑(如 Nexus 7)上崩溃,但不是全部。
我的代码:
public static boolean getNetworkState(Context pContext)
{
ConnectivityManager connect = (ConnectivityManager)pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connect != null)
{
if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())
{
return true;
}
else
{
return false;
}
}
else
return false;
}
public static boolean getWifiState(Context context)
{
ConnectivityManager connect = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if ( connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting())
{
return true;
}
else
{
return false;
}
}
连接检查:
if(Commons.getNetworkState(this) || Commons.getWifiState(this))
{
loadData();
}
else
{
Commons.getConnectivityErrorMessage(this);
}
通常,如果平板电脑不支持网络连接,“getNetworkState”中的“connectivity”对象应该为空,但似乎并非如此,因为它在线崩溃
if (connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting())
这里有什么问题?权限已经写好了
编辑:不幸的是,我没有平板电脑来测试这个问题。我只是在开发者控制台上有用户报告。使用 Nexus 7 的用户报告了以下报告:
java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.MeteocielActivity}: java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to resume activity {com.meteociel.fr/com.meteociel.fr.activities.HomeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:178)
at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:523)
at android.app.ActivityGroup.onResume(ActivityGroup.java:61)
at com.meteociel.fr.activities.MeteocielActivity.onResume(MeteocielActivity.java:69)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
... 12 more
Caused by: java.lang.NullPointerException
at com.meteociel.fr.classes.Commons.getNetworkState(Commons.java:36)
at com.meteociel.fr.activities.HomeActivity.onResume(HomeActivity.java:98)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
... 19 more
编辑 2:我使用的所有权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />