2012-07-18 84 views
0

我想在我的android應用程序中檢索我的adroid設備的設備ID。然而,存在錯誤後,我在我的程序中添加以下行的程序無法啓動:獲取Android設備ID時出錯

String ts = Context.TELEPHONY_SERVICE; 
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(ts); 

從logcat的,它說,問題出現fromt下聯:

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 

我認爲這是造成權限問題,但我已經添加了清單文件,下面一行:

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

那麼,請問我該如何解決這個問題?

回答

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?

+0

雅,我已經閱讀過此頁面,實際上,我的方法與您提到的方法相同。我知道我可以通過getDeviceID()方法獲取設備ID,但是,當前問題是在添加以下行之後:TelephonyManager mTelephonyMgr =(TelephonyManager)getSystemService(ts);那麼這些錯誤就存在了,這就是爲什麼我想尋求幫助,因爲我已經允許我的程序 – Conrad 2012-07-18 04:04:17

+0

現在沒關係,我已經改變了我的設計,無論如何,謝謝你幫助我。 – Conrad 2012-07-18 06:32:02