0

我正在解析一个 xml 文件,但我不断收到一个空指针异常。我不知道这里的空指针是:(

列表视图.java

static String URL = "https://dl.dropbox.com/u/####/wosm-library-EN.xml";    
static final String KEY_ITEM = "item"; 
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DESCRIPTION = "description";
static final String KEY_FILESIZE = "filesize";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_DOCUMENT_URL = "document_url";
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);



ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.library);

    sharedPrefs.getBoolean("french", false); {
                  URL="https://dl.dropbox.com/u/######/wosm-library-EN.xml";        

        }
         sharedPrefs.getBoolean("french", true);
            {
                  URL="https://dl.dropbox.com/u/######/wosm-library-FR.xml";        
        } 



    ArrayList<HashMap<String, String>> libraryList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); 
    Document doc = parser.getDomElement(xml); 

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);

    for (int i = 0; i < nl.getLength(); i++) {

        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);

        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
        map.put(KEY_FILESIZE, parser.getValue(e, KEY_FILESIZE));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
        map.put(KEY_DOCUMENT_URL, parser.getValue(e, KEY_DOCUMENT_URL));

        libraryList.add(map);
    }


    list=(ListView)findViewById(R.id.list);         
    adapter=new LazyAdapter(this, libraryList);        
    list.setAdapter(adapter);





    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.artist)).getText().toString();
            //String thumb_url = ((ImageView)view.findViewById(R.id.list_image)).getImageMatrix().toString();
            //String thumb_url = ((TextView) view.findViewById(R.id.thumburl)).getText().toString();            
            String filesize = ((TextView) view.findViewById(R.id.duration)).getText().toString();
            String thumburl = ((TextView) view.findViewById(R.id.imgurl)).getText().toString();
            String doc_url = ((TextView) view.findViewById(R.id.documenturl)).getText().toString();

            Intent in = new Intent(CustomizedListView.this, org.scouts.library.SingleMenuItem.class);
            in.putExtra(KEY_TITLE, title);
            in.putExtra(KEY_DESCRIPTION, description);
            //in.putExtra(KEY_THUMB_URL, thumb_url);
            in.putExtra(KEY_FILESIZE, filesize);
            in.putExtra(KEY_THUMB_URL, thumburl);
            in.putExtra(KEY_DOCUMENT_URL, doc_url);
            startActivity(in);

        }
    });     
}   
}

在我添加了偏好变量后,我才开始得到空指针异常

4

1 回答 1

1

将 SharedPreferences 行放入您的 onCreate() 方法中。

你还需要使用if(){//do something} else {//do something else}

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
if(sharedPrefs.getBoolean("french", false))
{
   URL="https://dl.dropbox.com/u/######/wosm-library-EN.xml";        
}
else
{
   URL="https://dl.dropbox.com/u/######/wosm-library-FR.xml";        
} 

更好的方法是使用 Dropbox URL 的资源字符串并提供不同的本地化。那么你根本不需要这个代码。

于 2012-07-04T09:19:57.650 回答