我试图在列表视图中创建一个世界创建者,以保存世界名称。但是,着色首选项会导致程序在活动打开之前崩溃。为什么会这样?没有共享的偏好,这是完美的。有任何想法吗? (在列表视图上单击是未完成的,不要担心。)最值得注意的错误是数组适配器上的null pointerexception,storage == 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);
}
}
}
答案 0 :(得分:0)
在您的活动中尝试按照首选项init:
private SharedPreferences pref = null;
...
pref = PreferenceManager.getDefaultSharedPreferences(this);
String worldString= prefs.getString("worldString", "No worlds found.");
...
答案 1 :(得分:0)
您在适配器下添加了列表视图的共享首选项和数组,因此无法首先创建列表视图。只需将变量放在适配器上。