2013-04-22 81 views
0

我已經被賦予了將XML數據解析到基本列表視圖然後到文本視圖中的任務,但是當編輯代碼有目的地爲練習提供時,我的應用程序在按下「登錄」按鈕後會停止主要活動。感謝所有讀過這篇文章的人,我真的很茫然,我一直在努力調整它幾個小時。Android應用程序運行時異常問題。

我想知道是否有人可以找到問題。

package com.example.beaconhomesproject; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.sax.Element; 
import android.sax.EndElementListener; 
import android.sax.EndTextElementListener; 
import android.sax.RootElement; 
import android.util.Xml; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class Bill extends Activity { 
private ArrayList<BillListView> bill; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bill); 

    // Download 
    new DownloadBillListTask() 
      .execute("http://www.fcet.staffs.ac.uk/pfb1/IMWTAssignment/bills.xml"); 
    // Adds a CLick Listener to the list view 
    ListView listView = (ListView) findViewById(R.id.bill_list); 
    listView.setOnItemClickListener(new ListView.OnItemClickListener() { 

     // Opens a new Activity when an item is clicked 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      Intent intent = new Intent(view.getContext(), `enter code here`BillDetails.class); 
      intent.putExtra("BillListView", bill.get(position)); 
      startActivity(intent); 
     } 
    }); 
} 

// get xml 
public ArrayList<BillListView> ParseBillsFromXML(String XMLURL) { 
    final ArrayList<BillListView> values = new ArrayList<BillListView>(); 
    final BillListView LeBill = new BillListView(); 

    // Convert our string into a URL 
    URL feedUrl; 
    try { 
     feedUrl = new URL(XMLURL); 
    } catch (MalformedURLException e) { 
     throw new RuntimeException(e); 
    } 

    // Get the RootElement of our XML file which is Students 
    RootElement root = new RootElement("Bills"); 
    Element bill = root.getChild("bill"); // Then list our element we want, 
              // so a single Student 

    // Our End Element Listener event fires when we get the end of a 
    // complete Student tag 
    bill.setEndElementListener(new EndElementListener() { 
     @Override 
     public void end() { 
      values.add(LeBill.copy()); 
     } 
    }); 

    // All of the End Text Element Listeners get the body of the child tags 
    // of a bill 
    bill.getChild("date").setEndTextElementListener(
      new EndTextElementListener() { 
       @Override 
       public void end(String body) { 
        LeBill.setDate(body); 
       } 
      }); 
    bill.getChild("type").setEndTextElementListener(
      new EndTextElementListener() { 
       @Override 
       public void end(String body) { 
        LeBill.setType(body); 
       } 
      }); 
    bill.getChild("price").setEndTextElementListener(
      new EndTextElementListener() { 
       @Override 
       public void end(String body) { 
        LeBill.setPrice(body); 
       } 
      }); 

    // Now we have set up all our event listeners, we can start to parse the 
    // document 
    try { 
     Xml.parse(feedUrl.openConnection().getInputStream(), 
       Xml.Encoding.UTF_8, root.getContentHandler()); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 

    // We can then return all our returns values after parsing 
    return values; 
} 

// This is an inner class which runs Asynchronously (In the background) 
// It needs to be told the three types it accepts 
// String is the type that is passed to doInBackground 
// Void is the type that is used if we report any progress (We don't in this 
// case so its Void!) 
// ArrayList<Student> is the data type that is returned from our background 
// task and is passed to final function 
class DownloadBillListTask extends 
     AsyncTask<String, Void, ArrayList<BillListView>> { 

    // This is the code that runs in the background - We can't update any UI 
    // in this function! 
    // String... basically means we are passing in a list of strings but we 
    // don't specify how many! 
    @Override 
    protected ArrayList<BillListView> doInBackground(String... arg0) { 

     // This calls our Parse function above 
     return ParseBillsFromXML(arg0[0]); 
    } 

    // This executes back on the UI thread so we can actually update the UI 
    // here 
    @Override 
    protected void onPostExecute(ArrayList<BillListView> result) { 
     // Save our result to our class level students ArrayList 
     bill = result; 

     // Set the list view to display the list we just downloaded! 
     ListView listView = (ListView) findViewById(R.id.bill_list); 

     ArrayAdapter<BillListView> adapter = new ArrayAdapter<BillListView>(
       getBaseContext(), android.R.layout.simple_list_item_1, 
       result); 

     listView.setAdapter(adapter); 
    } 

} 

public void movemain(View view) { 
    Intent intent = new Intent(this, MainActivity.class); 
    startActivity(intent); 
} 


} 

的XML:

<ImageView 
    android:id="@+id/waterlogo" 
    android:layout_width="100dp" 
    android:layout_height="80dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:contentDescription="@string/welcome" 
    android:scaleType="fitXY" 
    android:src="@drawable/water" /> 


    <View 
     android:id="@+id/viewyview" 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:background="#e47c0b" 
     android:layout_below="@id/waterlogo"/> 
     <ListView 
    android1:id="@+id/bill_list" 
    android1:layout_width="match_parent" 
    android1:layout_height="match_parent" 
    android1:layout_alignTop="@+id/viewyview" /> 

    <Button 
    android:id="@+id/backbutton" 
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="@android:color/transparent" 
    android:gravity="center" 
    android:onClick="moveback" 
    android:text="@string/back" 
    android:textColor="#e47c0b" /> 



</RelativeLayout> 

登錄貓:

>04-22 14:39:14.965: E/AndroidRuntime(1053): FATAL EXCEPTION: AsyncTask #3 
>04-22 14:39:14.965: E/AndroidRuntime(1053): java.lang.RuntimeException: An error occured while executing doInBackground() 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.lang.Thread.run(Thread.java:856) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): Caused by: java.lang.RuntimeException: android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'Bills', Got: 'bills' 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at com.example.beaconhomesproject.Bill.ParseBillsFromXML(Bill.java:103) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at com.example.beaconhomesproject.Bill$DownloadBillListTask.doInBackground(Bill.java:128) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at com.example.beaconhomesproject.Bill$DownloadBillListTask.doInBackground(Bill.java:1) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): ... 4 more 
>04-22 14:39:14.965: E/AndroidRuntime(1053): Caused by: android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'Bills', Got: 'bills' 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.sax.RootElement$Handler.startRoot(RootElement.java:145) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.sax.RootElement$Handler.startElement(RootElement.java:116) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatParser.startElement(ExpatParser.java:143) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:321) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at android.util.Xml.parse(Xml.java:84) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): at com.example.beaconhomesproject.Bill.ParseBillsFromXML(Bill.java:100) 
>04-22 14:39:14.965: E/AndroidRuntime(1053): ... 8 more 

回答

1

這裏是你的問題:

14:39:14.965: E/AndroidRuntime(1053): Caused by: java.lang.RuntimeException: android.sax.BadXmlException: Line 1: Root element name does not match. Expected: 'Bills', Got: 'bills'

所以:

RootElement root = new RootElement("bills");

+0

非常感謝!我按照你所說的改變了這條線,但那不起作用,我檢查了根和子標籤元素,並且我有一張厚顏無恥的大寫字母,在那裏不應該有。 – Grubbery 2013-04-22 15:02:48

+0

哦,你是正確的,我編輯了我的帖子。 – Nima 2013-04-22 15:05:13

相關問題