Search code examples
androidxmlpreference

Android XML Parsing & Preferences Error


Im parsing an xml file and i keep getting a nullpointer exception. I don't know here the null pointer is :(

ListView.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);

        }
    });     
}   
}

i only started getting the nullpointer excepetion after i added the preference variable


Solution

  • Put the SharedPreferences line into you onCreate() method.

    Also you need to use 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";        
    } 
    

    A far better way to do this would be to use a resource string for the Dropbox URL and provide different localizations. Then you would not need this code at all.