0

我想问一些关于 android 编程的问题。我创建了一个从 rss xml 文件返回显示到 android 的类,但我得到了一些错误

04-08 14:37:19.162: E/AndroidRuntime(381): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.xmlreader/com.example.xmlreader.MainActivity}: java.lang.NullPointerException

代码:

static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

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

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

    XMLParser parser = new XMLParser();
    String xml = parser.getXMLFromURL(URL); // getting XML
    Document doc = parser.getDomElem(xml); // getting DOM element

(*) NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_DESC, description);
            startActivity(in);

        }
    });
}

第 38 行或第 38 行中的错误给我 (*) 这个标记...请帮助我...

4

2 回答 2

0

如果空指针出现在下一行(带 *):

 (*) NodeList nl = doc.getElementsByTagName(KEY_ITEM); //i reckon the nullpointer is coming up on the doc object...

然后我估计要检查从 getXMLFromURL 方法中检索到的 xml 是否正确?:

XMLParser parser = new XMLParser();
String xml = parser.getXMLFromURL(URL); // getting XML   (Is this correctly retreived?
Document doc = parser.getDomElem(xml); // getting DOM element  (This is returning null)

如需更多帮助,请分享您的 XMLParser 类

于 2013-04-08T08:42:06.280 回答
0
  1. 您需要使用ThreadAsyncTask来执行“网络操作”。否则你会得到 NetworkOnMainThreadException。读这个

  2. 向 AndroidManifest.xml 添加权限。

    <uses-permission android:name="android.permission.INTERNET"/>

    使用线程的示例

     menuItems = new ArrayList<HashMap<String, String>>();
     final XMLParser parser = new XMLParser();
    
     Thread th = new Thread(new Runnable() {
        @Override
        public void run() {
            xml = parser.getXmlFromUrl(URL);
    
            handler.post(new Runnable() { //create an object of Handler class in onCreate() - (android.os.Handler)
                @Override
                public void run() {
                    Document doc = parser.getDomElement(xml);
                    nl =doc.getElementsByTagName(KEY_ITEM);
                    //other code inside onCreate() - for loop
                }
            });
       }
    }
    th.start();
    
    SimpleAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
                new String[]{KEY_NAME, KEY_COST, KEY_DESCRIPTION}, new int[]{R.id.name, R.id.cost, R.id.description});
    
    setListAdapter(adapter);
    
于 2014-01-15T08:31:53.403 回答