如果屏幕旋转两次,应用程序崩溃,logcat:java.lang.NullPointerException

时间:2015-09-28 02:21:59

标签: android

问题:当用户开始游戏时,他有6个单词可供选择,这6个单词是从列表中随机选择的。第一次活动中断(例如屏幕旋转),一切正常。如果屏幕再次旋转,应用程序崩溃并且logcat会吐出以下结果:

  java.lang.NullPointerException: Attempt to invoke virtual method '
java.lang.Object java.util.LinkedList.get(int)' on a null object reference

以下是相关代码:

 public class GameActivity extends Activity {
    protected String rWord1;
    protected String rWord2;
    protected String rWord3;
    protected TextView word1;
    protected LinkedList<String> mCopy;
    protected TextView word2;
     /// all the way to word6

    @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    if (savedInstanceState != null) {
         rWord1 = savedInstanceState.getString("word1");
         rWord2 = savedInstanceState.getString("word2");
         rWord3 = savedInstanceState.getString("word3");


         word1 = (TextView) findViewById(R.id.word1);
         word1.setText(mSavedList.get(1));

        word2 = (TextView) findViewById(R.id.word2);
        word2.setText(mSavedList.get(2));
         //up until word6

     }

   else {
    //take the list of all the words in the LinkedList mCopy, 
     randomize it, and pick the first         six.

   }

}// end of onCreate method 

  @Override
   public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putString("word1", mCopy.get(1));
        savedInstanceState.putString("word2", mCopy.get(2));
        savedInstanceState.putString("word3", mCopy.get(3));


    super.onSaveInstanceState(savedInstanceState);
}

我想我知道问题所在:当第一次创建活动并且屏幕旋转时,列表(mCopy)将保存在包中。但是,当屏幕第二次旋转时,此时mCopy为null,因为它没有机会被创建(因为if(savedInstanceState!= null)条件)。我不知道如何解决这个错误。我尝试创建字符串来引用列表,

 String sWord1 = mCopy.get(1),  

然后将此String存储在包中,但是当我运行应用程序并旋转屏幕两次时,应用程序不会崩溃,但所有单词都会从视图中消失。

编辑我尝试了下面一些评论员建议的方法,更新的代码是:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_activity);


        Collection<String> wordList = new LinkedList<String>();

        wordList.add("ant");
        wordList.add("almond");
        /// lots of words

     mCopy = new LinkedList<String>(wordList);
    if (savedInstanceState != null) {

            rWord1 = savedInstanceState.getString("word1");
            rWord2 = savedInstanceState.getString("word2");


            word1 = (TextView) findViewById(R.id.word1);
            word1.setText(rWord1);

            word2 = (TextView) findViewById(R.id.word2);
            word2.setText(rWord2);

        }

        else {

            Collections.shuffle(mCopy);

        word1 = (TextView) findViewById(R.id.word1);
        word1.setText(mCopy.get(1));

现在,出现了一个新问题:第一次屏幕旋转时,活动会保留单词。如果屏幕再次旋转,则活动会更改单词。但是,任何后续更改都将导致数据得以维护。

例如: 在活动开始时:在屏幕上出现的词是&#34;痛苦&#34; 在第一个屏幕旋转:&#34;疼痛&#34; 在第二个屏幕旋转:&#34;蓝色&#34; 第三轮:&#34;蓝&#34;

基本上,似乎代码在第一次屏幕旋转之后而不是之前运行。

此外,如果用户针对3个不同的玩家打开3个游戏,那么在任何游戏中初始单词的含义并不重要。在第二个屏幕旋转时,单词始终显示为&#34;蓝色&#34;对阵所有3名球员。

2 个答案:

答案 0 :(得分:1)

将此configChanges属性添加到androidManifest.xml中,即使屏幕旋转,也会保留所有活动状态和资源。这将有所帮助,因为每次旋转屏幕时,旧活动都会被关闭并重新创建,这意味着可能会有其他资源无法立即重新创建,因此它将显示空指针。

  <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize|screenLayout"

答案 1 :(得分:0)

作为您的logcat,您应该将它添加到onCreate(...)方法

  mCopy = new LinkedList();

希望这个帮助

相关问题