1

我想知道我在启动我的应用程序时是否有移动网络:

final ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

在我的开始应用程序类中:

public class StartApplication extends Application {

我遇到了一些错误:

04-23 14:41:36.683: E/AndroidRuntime(1280): FATAL EXCEPTION: main
04-23 14:41:36.683: E/AndroidRuntime(1280): java.lang.RuntimeException: Unable to instantiate application com.chipsat.sigue.StartApplication: java.lang.NullPointerException
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.os.Looper.loop(Looper.java:137)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at java.lang.reflect.Method.invoke(Method.java:511)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at dalvik.system.NativeStart.main(Native Method)
04-23 14:41:36.683: E/AndroidRuntime(1280): Caused by: java.lang.NullPointerException
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.content.ContextWrapper.getSystemService(ContextWrapper.java:495)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at com.chipsat.sigue.StartApplication.<init>(StartApplication.java:82)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at java.lang.Class.newInstanceImpl(Native Method)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at java.lang.Class.newInstance(Class.java:1319)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.Instrumentation.newApplication(Instrumentation.java:983)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.Instrumentation.newApplication(Instrumentation.java:968)
04-23 14:41:36.683: E/AndroidRuntime(1280):     at android.app.LoadedApk.makeApplication(LoadedApk.java:499)
04-23 14:41:36.683: E/AndroidRuntime(1280):     ... 11 more
4

1 回答 1

4

I guess the problem is you call getSystemService before onCreate. You can check Exception on getSystemService(Context.AUDIO_SERVICE) and System services not available to Activities before onCreate?. Basically, the Context is not available yet when you use it.

Edit: I'm not 100% sure but you can try:

ConnectivityManager connMgr;
android.net.NetworkInfo mobileInfo

//in onCreate
connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

Note: the code is not tested.

于 2013-04-23T15:20:22.963 回答