我对 Android 应用程序开发非常陌生。在我正在制作的这个练习应用程序中,当应用程序启动时,它首先检查是否启用了位置提供程序以及是否启用了该数据。
我已经在 Androidmanifest 中设置了权限 ACCESS_NETWORK_STATE 但应用程序仍然崩溃。
我想就如何解决这个问题征求一些建议。
谢谢!
这是我的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locMan = (LocationManager) getSystemService (LOCATION_SERVICE);
ConnectivityManager conMan = (ConnectivityManager) getSystemService (CONNECTIVITY_SERVICE);
boolean statCheck = locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean dataConCheck = conMan.getActiveNetworkInfo().isConnected();
if (statCheck == false) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
if (dataConCheck == false) {
Intent intent = new Intent (Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
}
05-11 00:58:52.349: W/dalvikvm(1354): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-11 00:58:52.359: E/AndroidRuntime(1354): FATAL EXCEPTION: main
05-11 00:58:52.359: E/AndroidRuntime(1354): java.lang.RuntimeException: Unable to start activity ComponentInfo{cheapapps.productivity.inviter/cheapapps.productivity.inviter.MainActivity}: java.lang.NullPointerException
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.os.Looper.loop(Looper.java:130)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-11 00:58:52.359: E/AndroidRuntime(1354): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 00:58:52.359: E/AndroidRuntime(1354): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 00:58:52.359: E/AndroidRuntime(1354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-11 00:58:52.359: E/AndroidRuntime(1354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-11 00:58:52.359: E/AndroidRuntime(1354): at dalvik.system.NativeStart.main(Native Method)
05-11 00:58:52.359: E/AndroidRuntime(1354): Caused by: java.lang.NullPointerException
05-11 00:58:52.359: E/AndroidRuntime(1354): at cheapapps.productivity.inviter.MainActivity.onCreate(MainActivity.java:25)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 00:58:52.359: E/AndroidRuntime(1354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-11 00:58:52.359: E/AndroidRuntime(1354): ... 11 more