gpt4 book ai didi

java - 当我启动应用程序时尝试获取 Android_ID 崩溃

转载 作者:行者123 更新时间:2023-12-01 18:05:16 26 4
gpt4 key购买 nike

这是获取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.access$900(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.access$900(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);

而且,也不起作用。顺便说一句,我很清楚这不是识别用户的完美方式,因为号码可能会因恢复出厂设置或更换手机以及其他几种方式而发生变化。

最佳答案

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() 之后初始化它。

关于java - 当我启动应用程序时尝试获取 Android_ID 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36903090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com