Activity.onCreate()中的Android应用上下文为null>

问题描述 投票:0回答:1

此活动中的代码

public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    DisplayMetrics metrics = App.getAppContext().getResources().getDisplayMetrics(); //crash
}

有时会产生NullPointerException

这是自定义App类:

public class App extends Application
{
    private static Context context;

    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getAppContext()
    {
        return context;
    }
}

如何固定NullPointerException

此活动中的代码public void onCreate(Bundle bundle){super.onCreate(bundle); DisplayMetrics指标= App.getAppContext()。getResources()。getDisplayMetrics(); // crash}有时候...

android nullpointerexception android-context
1个回答
0
投票

替换:

© www.soinside.com 2019 - 2024. All rights reserved.