我正在开发我的第一个 Android 应用程序。在我决定添加一个功能来保存和加载 OnCreate 和 onRestart 上的对象和设置之前,它工作得很好。之后,应用程序在启动时崩溃。这是我从 OnCreate 调用的 setSettings。
public void setSettings(){
SharedPreferences settings = getSharedPreferences("Prefs",0);
newGame = settings.getBoolean("newGame", true);
autoDecide = settings.getBoolean("autoDecide", true);
saved = settings.getBoolean("saved", false);
if (saved){
player1 = readPlayer("Player 1");
player2 = readPlayer("Player 2");
((TextView)findViewById(R.id.Player1name)).setText(""+player1.name);
((TextView)findViewById(R.id.Player2name)).setText(""+player2.name);
((TextView)findViewById(R.id.player1wins)).setText(""+player1.totalWins);
((TextView)findViewById(R.id.player2wins)).setText(""+player2.totalWins);
((TextView)findViewById(R.id.Player1life)).setText(""+player1.life);
((TextView)findViewById(R.id.Player2life)).setText(""+player2.life);
}
}
在我保存东西之前,保存的布尔值应该是错误的,但我认为它不起作用。这是它调用的可能问题所在的 readPlayer。
public Player readPlayer(String loc) {
//FileInputStream fis;
try {
BufferedInputStream buf = new BufferedInputStream( openFileInput(loc));
ObjectInputStream stream = new ObjectInputStream(buf);
Player re = (Player) stream.readObject();
stream.close();
buf.close();
return re;
} catch (FileNotFoundException e) {
//e.printStackTrace();
return new Player(loc);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return new Player(loc);
}
这是我在崩溃时得到的 logCat。
05-26 03:49:05.739: E/AndroidRuntime(391): java.lang.RuntimeException: Unable to start activity ComponentInfo{jjcard.app.lifecount/jjcard.app.lifecount.Life_counterActivity}: java.lang.NullPointerException
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.os.Looper.loop(Looper.java:132)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-26 03:49:05.739: E/AndroidRuntime(391): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 03:49:05.739: E/AndroidRuntime(391): at java.lang.reflect.Method.invoke(Method.java:491)
05-26 03:49:05.739: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-26 03:49:05.739: E/AndroidRuntime(391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-26 03:49:05.739: E/AndroidRuntime(391): at dalvik.system.NativeStart.main(Native Method)
05-26 03:49:05.739: E/AndroidRuntime(391): Caused by: java.lang.NullPointerException
05-26 03:49:05.739: E/AndroidRuntime(391): at jjcard.app.lifecount.Life_counterActivity.setSettings(Life_counterActivity.java:72)
05-26 03:49:05.739: E/AndroidRuntime(391): at jjcard.app.lifecount.Life_counterActivity.onCreate(Life_counterActivity.java:42)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.Activity.performCreate(Activity.java:4397)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
05-26 03:49:05.739: E/AndroidRuntime(391): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
我不知道您是否需要创建播放器,但在这里
public Player(String nameN){
name = nameN;
life = 8000;
totalWins = 0;
totalLosses = 0;
wins = new HashMap<String, Integer>();
}
每次我启动它时都会发生这种情况,并且我尝试了一些事情,正如您可能从代码中看到的那样,没有成功。我试着四处寻找,但找不到任何东西。希望大家能帮忙。
编辑:这是 LogCat 显示的第 72 行是问题所在。
((TextView)findViewById(R.id.Player1name)).setText(""+player1.name);
所以看起来你是对的,它可能会读取一个空对象,而不是像我想象的那样返回一个新玩家。而且由于我对编写和读取对象不太了解,所以我只使用了 defaultWriteObject 和 defaultReadObject
private void writeObject(ObjectOutputStream out) throws IOException{
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException{
in.defaultReadObject();
}
编辑:更改读/写并尝试检查名称是否为空后,它仍然不起作用。我什至尝试将文本设置为字符串,但它仍然给出 nullPointerException,所以猜测它与 FindViewById(R.id.Player1name) 有关系,但我还不知道具体是什么。我使用 ViewPagerIndicator 为不同的屏幕使用不同的视图,这可能与它有关吗?