2013-08-24 68 views
0

下面是我的活動的代碼,其中的錯誤是。如何解決這個錯誤的XML?

package com.pcriot.maxsoft.remotemediaplayer; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class SelectFileActivity extends ListActivity { 

    // All static variables 
    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_selectfile); 

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

     XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(URL); // getting XML 
     Document doc = parser.getDomElement(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.listview_style_1, 
       new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { 
         R.id.name, R.id.description, R.id.cost }); 

     setListAdapter(adapter); 

     ListView lv = getListView(); 

     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parnet, android.view.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.description)).getText().toString(); 

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

      } 
     }); 
    } 
} 

該代碼讀取一個XML文件並在列表視圖中添加值。

我開始使用Java編程,所以我不知道調試代碼,所以我不知道錯誤是什麼以及它不在哪裏,只是當我通過智能手機訪問此活動時,應用程序只是崩潰並關閉。

+0

PLZ後logcat的 – KOTIOS

+0

您可以發佈logcat的日誌崩潰(堆棧跟蹤) – Sushil

+0

發佈堆棧跟蹤。 –

回答

0

您正試圖在UIThread(主線程)中執行xml解析,這可能會導致崩潰。任何需要更長時間或網絡活動的任務都應該在其他線程中完成。如果你阻止更長時間的UI線程,應用程序崩潰。你在AsyncTask中完成這項工作。

+0

我該怎麼做? –

0

試試這個..

import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class NetActivity extends Activity { 

    String url = "http://api.androidhive.info/pizza/?format=xml"; 


    // Progress dialog 
    ProgressDialog pDialog; 

    ArrayList<String> title; 
    ArrayList<String> description; 
    ArrayList<String> id; 
    ArrayList<String> cost; 

    ItemAdapter adapter1; 

    ListView list; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_net); 

     list = (ListView) findViewById(R.id.list); 
     title = new ArrayList<String>(); 
     description = new ArrayList<String>(); 
     id = new ArrayList<String>(); 
     cost = new ArrayList<String>(); 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
      new XmlParsing(url).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null}); 
     else 
      new XmlParsing(url).execute(new String[]{null});  
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public class XmlParsing extends AsyncTask<String, Void, String> { 

     // variables passed in: 
     String urls; 
     // constructor 
     public XmlParsing(String urls) { 
      this.urls = urls; 
     } 

     @Override 
     protected void onPreExecute() { 
      pDialog = ProgressDialog.show(NetActivity.this, "Fetching Details..", "Please wait...", true); 
     } 


     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      URL url; 
      try { 

       url = new URL(urls); 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       Document doc = db.parse(new InputSource(url.openStream())); 

       doc.getDocumentElement().normalize(); 

       NodeList nodeList = doc.getElementsByTagName("item"); 

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

        Node node = nodeList.item(i);  

        Element fstElmnt = (Element) node; 
        NodeList nameList = fstElmnt.getElementsByTagName("name"); 
        Element nameElement = (Element) nameList.item(0); 
        nameList = nameElement.getChildNodes(); 
        title.add(""+ ((Node) nameList.item(0)).getNodeValue()); 

        System.out.println("name : "+((Node) nameList.item(0)).getNodeValue()); 


        Element fstElmnt1 = (Element) node; 
        NodeList nameList1 = fstElmnt1.getElementsByTagName("id"); 
        Element nameElement1 = (Element) nameList1.item(0); 
        nameList1 = nameElement1.getChildNodes(); 
        description.add(""+ ((Node) nameList1.item(0)).getNodeValue()); 

        System.out.println("id : "+ ((Node) nameList1.item(0)).getNodeValue()); 

        Element fstElmnt2 = (Element) node; 
        NodeList nameList2 = fstElmnt2.getElementsByTagName("cost"); 
        Element nameElement2 = (Element) nameList2.item(0); 
        nameList2 = nameElement2.getChildNodes(); 
        id.add(""+ ((Node) nameList2.item(0)).getNodeValue()); 

        System.out.println("cost : "+ ((Node) nameList2.item(0)).getNodeValue()); 

        Element fstElmnt3 = (Element) node; 
        NodeList nameList3 = fstElmnt3.getElementsByTagName("description"); 
        Element nameElement3 = (Element) nameList3.item(0); 
        nameList3 = nameElement3.getChildNodes(); 
        cost.add(""+ ((Node) nameList3.item(0)).getNodeValue()); 

        System.out.println("description : "+ ((Node) nameList3.item(0)).getNodeValue()); 

       } 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ParserConfigurationException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      // Now we have your JSONObject, play around with it. 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      String value = result; 



      adapter1 = new ItemAdapter(this); 
      list.setAdapter(adapter1); 

     } 

    } 


class ItemAdapter extends BaseAdapter { 

     final LayoutInflater mInflater; 

     private class ViewHolder { 
      public TextView title_text; 
      public TextView des_text; 
      public TextView id_text; 
      public TextView cost_text; 
     } 

     public ItemAdapter(XmlParsing xmlParsing) { 
      // TODO Auto-generated constructor stub 
      super(); 
      mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
     } 

     //@Override 
     public int getCount() { 
      return title.size(); 
     } 

     //@Override 
     public Object getItem(int position) { 
      return position; 
     } 

     //@Override 
     public long getItemId(int position) { 
      return position; 
     } 

     //@Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View view = convertView; 
      final ViewHolder holder; 
      if (convertView == null) { 
       view = mInflater.inflate(R.layout.mainpage_listitem_activity, parent, false); 
       holder = new ViewHolder(); 
       holder.title_text = (TextView) view.findViewById(R.id.title_text); 
       holder.des_text = (TextView) view.findViewById(R.id.des_text); 
       holder.id_text = (TextView) view.findViewById(R.id.id_text); 
       holder.cost_text = (TextView) view.findViewById(R.id.cost_text); 

       view.setTag(holder); 
      } else { 
       holder = (ViewHolder) view.getTag(); 
      } 

      holder.title_text.setText(""+title.get(position)); 


      holder.des_text.setText(""+description.get(position)); 
      holder.id_text.setText(""+id.get(position)); 
      holder.cost_text.setText(""+cost.get(position)); 

     return view; 
     } 
    } 

} 

mainpage_listitem_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
      <TextView 
       android:id="@+id/title_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"  
       android:text="title" 
       android:layout_margin="5dp" 
       android:textSize="22dp" 
       android:textColor="#000000"/> 

      <TextView 
       android:id="@+id/des_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"  
       android:gravity="center" 
       android:text="description " 
       android:layout_margin="5dp" 
       android:textSize="18dp" 
       android:textColor="#000000"/> 

      <TextView 
       android:id="@+id/id_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"  
       android:gravity="center" 
       android:text="id " 
       android:layout_margin="5dp" 
       android:textSize="18dp" 
       android:textColor="#000000"/> 

      <TextView 
       android:id="@+id/cost_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"  
       android:gravity="center" 
       android:text="cost " 
       android:layout_margin="5dp" 
       android:textSize="18dp" 
       android:textColor="#000000"/> 
</LinearLayout> 

activity_net.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 


    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
       > 

    </ListView> 

</LinearLayout> 
+0

它沒有工作,感謝您的幫助,我認爲我做錯了什麼。 –

+0

它會工作後,我檢查然後只有我發佈給你... – Hariharan