java.lang.RuntimeException:无法启动活动ComponentInfo错误

时间:2013-04-08 08:32:28

标签: android

我想问一些关于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行或第i行的错误是给(*)这个标记...... 请求帮助我...

2 个答案:

答案 0 :(得分:0)

如果nullpointer出现在下面的行(带*):

 (*) 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类

答案 1 :(得分:0)

  1. 您需要使用主题 AsyncTask 来执行“网络操作”。否则你将获得NetworkOnMainThreadException。 read this

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