XMLデータを基本的なリストビューに解析してからテキストビューに解析する割り当てが与えられましたが、問題は、演習用に意図的に提供されたコードを編集しているときに、メインの「ログイン」ボタンを押すとアプリケーションが停止することですアクティビティ。これを読んでくれた人に感謝します。私はここで本当に途方に暮れています。何時間も微調整しようとしてきました。
誰かが問題を見つけることができるかどうか疑問に思っていました。
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