gpt4 book ai didi

java - Android Activity 上下文 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:23:43 25 4
gpt4 key购买 nike

我在 Google Play 开发者控制台上收到一些关于 Activity 上下文或应用程序上下文中的 NPE 的奇怪错误报告。我无法在我的设备上重现此错误,并且我知道它在大多数设备上运行良好。

这是我的主要 Activity,是应用程序启动时首先打开的一个:

public class ActSplash extends Activity {
private Sync sync;
private Context ctx;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_splash);

do {
ctx = this.getApplicationContext(); //Using just "this" doesn't work either.
} while (ctx==null);

sync = Sync.getInstance(ctx); //Apparently ctx is null at this point in some devices
}

这是我的同步类(单例)

public class Sync {
private Context ctx;
private static Sync INSTANCE = null;

public static Sync getInstance(Context ctx) {
if (INSTANCE == null) createInstance(ctx);
return INSTANCE;
}

private synchronized static void createInstance(Context ctx) {
if (INSTANCE == null) {
INSTANCE = new Sync(ctx);
}
}

private Sync(Context _ctx)
{
ctx = _ctx;
//NPE on following line
WifiManager wifiMan = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
...
}

这是堆栈跟踪:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.xxx.xxx/com.xxx.xxx.ActSplash}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2178)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xxx.xxx.Sync.<init>(SourceFile:82)
at com.xxx.xxx.Sync.void createInstance(android.content.Context)(SourceFile:67)
at com.xxx.xxx.Sync.com.xxx.xxx.Sync getInstance(android.content.Context)(SourceFile:72)
at com.xxx.xxx.ActSplash.void onCreate(android.os.Bundle)(SourceFile:40)
at android.app.Activity.performCreate(Activity.java:5058)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2104)
... 11 more

在调用之前我该怎么做才能确保我有 ctx!=null:

sync = Util_Sync.getInstance(ctx);

最佳答案

因为你的代码不会改变

do {
ctx = this.getApplicationContext(); //Using just "this" doesn't work either.
} while (ctx==null);

如果无法获取ctx,这将导致无限循环。

Activity 是一个上下文!请参阅http://developer.android.com/reference/android/content/Context.html

Known Indirect Subclasses AbstractInputMethodService, AccessibilityService, AccountAuthenticatorActivity, ActionBarActivity, Activity, ActivityGroup, AliasActivity, Application, BackupAgent, BackupAgentHelper, ContextThemeWrapper, and 23 others.

尝试

ctx = this;

更新

在发布您的同步代码之后

private Context ctx;

从未使用过,因此将其删除。

关于java - Android Activity 上下文 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21895625/

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