0

我试图在保存世界名称的列表视图中创建世界创建者。但是阴影偏好会导致程序在活动打开之前崩溃。为什么会这样?没有共享偏好就很好了。有任何想法吗?(在列表视图单击未完成,不用担心。)最值得注意的错误是数组适配器的空指针异常、存储 == 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);
        }
    }
}
4

2 回答 2

0

在您的活动中尝试遵循首选项初始化:

private SharedPreferences pref = null;

...

pref = PreferenceManager.getDefaultSharedPreferences(this);

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

...
于 2013-08-03T19:02:19.210 回答
0

您为适配器下的列表视图添加了共享首选项和数组,因此无法首先创建列表视图。只需将变量放在适配器顶部即可。

于 2013-08-09T00:14:06.950 回答