试图通过应用程序获取 Android_ID 当我启动它时崩溃

Trying to get Android_ID bu the App crashes when i start it

这是获取 AndroidID 的代码:

String userID = Settings.Secure.getString(getApplicationContext()
.getContentResolver(), Settings.Secure.ANDROID_ID);

当我启动应用程序时,我得到以下 logcat 评论:

04-28 01:30:02.012 9328-9328/com.hubermoritz.beercounter.beercounter02 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.hubermoritz.beercounter.beercounter02, PID: 9328
                                                                                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hubermoritz.beercounter.beercounter02/com.hubermoritz.beercounter.beercounter02.MainActivity}: java.lang.NullPointerException
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
                                                                                         at android.app.ActivityThread.access0(ActivityThread.java:174)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:146)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5593)
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                                      Caused by: java.lang.NullPointerException
                                                                                         at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
                                                                                         at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)
                                                                                         at java.lang.Class.newInstanceImpl(Native Method)
                                                                                         at java.lang.Class.newInstance(Class.java:1208)
                                                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470) 
                                                                                         at android.app.ActivityThread.access0(ActivityThread.java:174) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:146) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5593) 
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                                         at dalvik.system.NativeStart.main(Native Method) 

04

尝试过:

public String userID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

同样,也没有用。顺便说一下,我很清楚这不是识别用户的完美方式,因为数字可能会因恢复出厂设置或更改 phone 以及其他几种方式而改变。

Caused by: java.lang.NullPointerException
  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:114)
  at com.hubermoritz.beercounter.beercounter02.MainActivity.<init>(MainActivity.java:48)

您正试图从字段初始值设定项调用从 Activity 继承的方法。不要这样做。通常,在 onCreate() 方法中调用 super.onCreate() 之前,不要调用 Activity 上的继承方法。欢迎您拥有一个字段,但在 super.onCreate().

之前不要初始化它

在构造函数完成之前,您不能使用 getContentResolver。在你的 onCreate 中设置这个变量,你应该没问题