2015-09-28 86 views
0

问题:当用户启动游戏时,他给出6个单词可供选择,这6个单词是从列表中随机选择的。第一次活动中断,(如屏幕旋转),一切正常。如果屏幕再旋转,应用程序崩溃和logcat的吐出了以下结果:如果屏幕旋转两次,应用程序崩溃logcat:java.lang.NullPointerException

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为空,因为它没有机会被创建(因为if(savedInstanceState!= null)条件)。我不知道如何解决这个错误。我尝试创建的字符串引用列表,

String sWord1 = mCopy.get(1), 

,然后存储此字符串捆绑代替,但后来当我运行的应用程序,并两次旋转屏幕,应用程序不会崩溃,但所有的字消失从视图。

编辑我试图通过下面的一些评论家的建议的方法,更新的代码是:

@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)); 

现在,有一个新的问题:第一次在屏幕旋转时,活动保留话。如果屏幕再次旋转,则活动会更改单词。但是,任何后续更改都会导致数据被保留。

例如: 上的活动开始:字出现在屏幕上的第一个屏幕旋转“疼痛” :“疼痛” 在第二屏幕旋转:“蓝” 上第三旋转:“蓝”

基本上,代码在第一个屏幕旋转之后而不是之前运行。另外,如果用户有3个游戏对3个不同的玩家开放,那么在任何游戏中最初的单词都是无关紧要的。在第二次屏幕旋转时,这个单词对所有3名玩家总是显示为“蓝色”。

+0

你在哪里初始化你的清单? – user1841702

+0

可能重复[什么是空指针异常,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – njzk2

+0

我在onCreate方法中初始化它,在 之后if(savedInstanceState!= null),我认为我应该在这个if语句之前这样做,对吧? – Frosty619

回答

1

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

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

优秀的建议,我会确定。如果其他解决方案无法正常工作,请使用此功能。 – Frosty619

+0

@ Frosty619这些简单的代码就像魔术一样:-)我们之前有同样的犹豫,但有时候让事情不复杂就是答案:-) – Cristiana214

0

随着你的logcat,你应该在的onCreate(...)方法

mCopy = new LinkedList(); 

希望这有助于

+0

所以你想让我在if(savedInstanceState!= null)方法之前初始化列表,对吗? – Frosty619

+0

@ Frosty619是的。您应该在使用前初始化它 –

+0

我提供了一个更新。请看看 – Frosty619

相关问题