我正在尝试在我的Android应用中检索我的adroid设备的设备ID。但是,在我的程序中添加以下行后,错误存在并且程序无法启动:
String ts = Context.TELEPHONY_SERVICE;
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(ts);
从logcat开始,它说问题来自第二行:
07-18 11:28:47.453: E/AndroidRuntime(5528): FATAL EXCEPTION: main
07-18 11:28:47.453: E/AndroidRuntime(5528): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.reader/android.reader.Reader}: java.lang.NullPointerException
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.os.Looper.loop(Looper.java:137)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 11:28:47.453: E/AndroidRuntime(5528): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:28:47.453: E/AndroidRuntime(5528): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 11:28:47.453: E/AndroidRuntime(5528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 11:28:47.453: E/AndroidRuntime(5528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 11:28:47.453: E/AndroidRuntime(5528): at dalvik.system.NativeStart.main(Native Method)
07-18 11:28:47.453: E/AndroidRuntime(5528): Caused by: java.lang.NullPointerException
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.reader.Reader.<init>(Reader.java:51)
07-18 11:28:47.453: E/AndroidRuntime(5528): at java.lang.Class.newInstanceImpl(Native Method)
07-18 11:28:47.453: E/AndroidRuntime(5528): at java.lang.Class.newInstance(Class.java:1319)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-18 11:28:47.453: E/AndroidRuntime(5528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
07-18 11:28:47.453: E/AndroidRuntime(5528): ... 11 more
我认为这是由权限问题引起的,但我已在Manifest文件中添加了以下行:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
那么,请问我该如何解决这个问题?
答案 0 :(得分:0)
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();
我猜这些答案可以帮到你。Is there a unique Android device ID?