http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singaporeからデータを取得します。「タイトル」、「開始時間」、「経度」、「緯度」の下のタグ。しかし、経度と緯度を追加した後、なぜ表示できなかったのかわかりません。
これはlogcatからのものです:
08-07 17:17:44.190: E/AndroidRuntime(23734): FATAL EXCEPTION: main
08-07 17:17:44.190: E/AndroidRuntime(23734): java.lang.NullPointerException
08-07 17:17:44.190: E/AndroidRuntime(23734): at com.example.eventfulmaptry.MainActivity$ItemAdapter.getView(MainActivity.java:147)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.AbsListView.obtainView(AbsListView.java:1618)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.ListView.measureHeightOfChildren(ListView.java:1241)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.ListView.onMeasure(ListView.java:1152)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.View.measure(View.java:8513)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3143)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.View.measure(View.java:8513)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3143)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.View.measure(View.java:8513)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.View.measure(View.java:8513)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3143)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.View.measure(View.java:8513)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.ViewRoot.performTraversals(ViewRoot.java:857)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.os.Looper.loop(Looper.java:130)
08-07 17:17:44.190: E/AndroidRuntime(23734): at android.app.ActivityThread.main(ActivityThread.java:3691)
08-07 17:17:44.190: E/AndroidRuntime(23734): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:17:44.190: E/AndroidRuntime(23734): at java.lang.reflect.Method.invoke(Method.java:507)
08-07 17:17:44.190: E/AndroidRuntime(23734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
08-07 17:17:44.190: E/AndroidRuntime(23734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
08-07 17:17:44.190: E/AndroidRuntime(23734): at dalvik.system.NativeStart.main(Native Method)
これは私のコードです:
public class MainActivity extends Activity {
ArrayList<String> title;
ArrayList<String> start_time;
ArrayList<String> latitude;
ArrayList<String> longitude;
ItemAdapter adapter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.list);
title = new ArrayList<String>();
latitude = new ArrayList<String>();
longitude = new ArrayList<String>();
try {
URL url = new URL(
"http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("event");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
title.add(""+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("start_time");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
start_time.add(""+ ((Node) websiteList.item(0)).getNodeValue());
NodeList websiteList1 = fstElmnt.getElementsByTagName("latitude");
Element websiteElement1 = (Element) websiteList1.item(0);
websiteList1 = websiteElement1.getChildNodes();
latitude.add(""+ ((Node) websiteList1.item(0)).getNodeValue());
NodeList websiteList2 = fstElmnt.getElementsByTagName("longitude");
Element websiteElement2 = (Element) websiteList2.item(0);
websiteList2 = websiteElement2.getChildNodes();
longitude.add(""+ ((Node) websiteList2.item(0)).getNodeValue());
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
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 lat_text;
public TextView long_text;
}
public ItemAdapter(Context context) {
// TODO Auto-generated constructor stub
super();
mInflater = (LayoutInflater)context.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_list,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.lat_text = (TextView) view.findViewById(R.id.lat_text);
holder.long_text = (TextView) view.findViewById(R.id.long_text);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title_text.setText(""+title.get(position));
holder.des_text.setText(""+Html.fromHtml(start_time.get(position)));
holder.lat_text.setText(""+Html.fromHtml(latitude.get(position)));
holder.long_text.setText(""+Html.fromHtml(longitude.get(position)));
return view;
}
}
}