07-08 17:04:34.978: E/AndroidRuntime(322): FATAL EXCEPTION: main
07-08 17:04:34.978: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gurpswu.gurps/com.gurpswu.gurps.Home}: java.lang.NullPointerException
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-08 17:04:34.978: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 17:04:34.978: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 17:04:34.978: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-08 17:04:34.978: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-08 17:04:34.978: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
07-08 17:04:34.978: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Player.getIntField(Player.java:153)
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Home.hudSetup(Home.java:102)
07-08 17:04:34.978: E/AndroidRuntime(322): at com.gurpswu.gurps.Home.onCreate(Home.java:25)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-08 17:04:34.978: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-08 17:04:34.978: E/AndroidRuntime(322): ... 11 more
我有一个游戏。当用户第一次打开应用程序并按下继续游戏按钮时......游戏将尝试使用以下代码从数据库中检索值。
stats.open();
playerId = stats.getIntField(stats.KEY_ROWID);
String playerName = stats.getStringField(stats.KEY_NAME);
String playerCity = stats.getStringField(stats.KEY_CITY);
playerHealth = stats.getIntField(stats.KEY_HEALTH);
playerEnergy = stats.getIntField(stats.KEY_ENERGY);
playerRank = stats.getIntField(stats.KEY_RANK);
playerCash = stats.getIntField(stats.KEY_CASH);
stats.close();
但是,这些数据库字段都没有填充...如何安装check ..so此代码仅在数据库存在时运行。如果它不存在则会要求玩家开始新游戏。
答案 0 :(得分:1)
在执行stats
后必须检查stats.open()
,您应该调用返回行数的内容。如果行为0,则可以设置条件。
或者你可以试试 NullPExe
try{
stats.open();
playerId = stats.getIntField(stats.KEY_ROWID);
String playerName = stats.getStringField(stats.KEY_NAME);
String playerCity = stats.getStringField(stats.KEY_CITY);
playerHealth = stats.getIntField(stats.KEY_HEALTH);
playerEnergy = stats.getIntField(stats.KEY_ENERGY);
playerRank = stats.getIntField(stats.KEY_RANK);
playerCash = stats.getIntField(stats.KEY_CASH);
stats.close();
} catch(NullPointerException e) { .. do needful or return null }