2013-08-03 75 views
0

我试图让一个世界的创造者在列表视图中保存世界的名字。但是,在活动打开之前,shaed首选项会导致程序崩溃。这是为什么发生?没有共享偏好,这是完全没问题的。有任何想法吗? (上列表视图中点击是未完成的,不用担心。)这是最显着的错误是在阵列适配器空pointerexception,存储== NULL,并跳过的帧共享首选项崩溃程序

package xxx.xxx.xxx; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.PopupWindow; 

public class WorldMenu extends ListActivity{ 
    SharedPreferences prefs; 
    String splitter; 
    String[] worldList; 
    PopupWindow worldNamer; 
    Drawable background; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(WorldMenu.this,        
      android.R.layout.simple_list_item_1, worldList)); 
     prefs = getSharedPreferences("worldString", 0); 
     splitter = "Create World\\\\\\\\\\\\\\\\\\\\\\\\\\" + 
      prefs.getString("worldString", "No worlds found."); 
     worldList = splitter.split("\\\\\\\\\\\\\\\\\\\\\\\\\\"); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     if(position == 0){ 
      worldNamer = new PopupWindow(this); 
      worldNamer.setBackgroundDrawable(null); 
     } 
    } 
} 
+1

那么堆栈跟踪说什么,*完全*?您的“最值得注意的错误”句子特别不明确...... –

+0

请在将来使用空格而不是制表符来格式化您的代码,顺便说一句。 –

+0

好的我下次还会这样做 – user2649191

回答

0

您添加的共享偏好和阵列使列表视图不能在第一时间创建的适配器下的列表视图。只需将变量放在适配器上。

0

在你的活动尝试跟随首INIT:

private SharedPreferences pref = null; 

... 

pref = PreferenceManager.getDefaultSharedPreferences(this); 

String worldString= prefs.getString("worldString", "No worlds found."); 

... 
+0

仍然不起作用 – user2649191