我想知道我是否有移动网络启动我的应用程序:
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
答案 0 :(得分:4)
我想问题是你在onCreate之前调用getSystemService。 您可以查看Exception on getSystemService(Context.AUDIO_SERVICE)和System services not available to Activities before onCreate?。基本上,当您使用Context时,Context仍然不可用。
编辑:我不是百分百确定,但您可以尝试:
ConnectivityManager connMgr;
android.net.NetworkInfo mobileInfo
//in onCreate
connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
注意:代码未经过测试。