我创建了一个扩展SQLiteOpenHelper的类(GameHelper)。我希望它是一个单例,所以我可以创建它并让它在运行时存储数据库的内容,所有其他类都引用它。
我遵循了建议here,(方法1)。
所以当我需要引用数据库时,我没有创建GameHelper的新实例,我调用了getInstance。以下是相关代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_info);
populate(getIntent().getStringExtra("game"), getIntent().getStringExtra("game"));
}
public void populate(String gameToGo, String game) {
DataTable game = gameHelper.getgame(game, gameToGo); //GameInfo.java:12
TextView tv = (TextView) findViewById(R.id.tableId);
String text = game.TABLE_ID;
tv.setText(text);
}
其中涉及GameHelper中的以下方法:
private static GameHelper sInstance;
public static synchronized GameHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new GameHelper(context.getApplicationContext()); //GameHelper.java:60
}
return sInstance;
}
问题出现在行sInstance = new GameHelper(context.getApplicationContext());
上,它会抛出NullPointerException,如下所示:
11-08 10:14:54.974 16214-16214/com.example.android.whichgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.whichgame, PID: 16214
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.whichgame/com.example.android.whichgame.GameInfo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.example.android.whichgame.GameHelper.getInstance(GameHelper.java:60)
at com.example.android.whichgame.GameInfo.<init>(GameInfo.java:12)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1062)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2155)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
我花了一整天的时间在互联网上搜索答案,虽然我发现了很多类似问题的答案,但我还没找到任何有帮助的东西。最好和最受欢迎的答案似乎是我需要在AndroidManifest中为android:name
设置<application>
,但这是另一个问题。我在Android Studio工作,当我尝试添加应用程序名称时,名称变为红色。我想我可能做错了!我的包(在清单中定义)是&#34; com.example.android.whichgame&#34;,尽管app本身(以及它所在的文件夹)被称为Whichgame。
所以我的问题是:我错过了什么导致getApplicationContext()在通过getInstance调用时踢出NPE?如果是因为我没有设置android:name,是什么导致Studio和编译器拒绝该名称?
答案 0 :(得分:0)
您在初始化阶段过早地将GameInfo
活动用作Context
。推迟在活动生命周期中需要有效Context
到onCreate()
或更高版本的初始化。