EventsActivity
是我的标签之一(3个标签有3个活动)。
问题是:
此活动可以显示为列表吗?如果是,我应该使用哪个列表(SimpleList
,ArrayList
等)?
我使用了ArrayList
HashMap
但意外停止了。我该如何解决这个问题?
我的代码中有一些例子:
班级
EventsActivity
延伸ListActivity
。这是我的一个标签。班级
EventDataSet
。 Getter和setter方法。班级
EventXMLHandler
。 XML Handler。类转换器。转换日期和时间。
布局文件夹:
eventitem.xml
listplaceholder.xml
EventsActivity.class
public class EventsActivity extends ListActivity {
EventDataSet eventDataSet = null;
ArrayList<HashMap<String, String>> mylist =
new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
try {
/** Handling XML */
/** Send URL to parse XML Tags */
/*URL sourceUrl */
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
} catch (Exception e) {
}
/** Get result from EventXMLHandler EventDataSet Object */
eventDataSet = EventXMLHandler.eventDataSet;
for (int i = 0; i < eventDataSet.getName().size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "Name: " + eventDataSet.getName().get(i));
map.put("createat", "Create-At: " +
Convertor.getDateTimeDDMMYY(
eventDataSet.getCreateat().get(i)));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist ,
R.layout.eventitem, new String[] { "name", "createat" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
}
listplaceholder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No data"/>
</LinearLayout>
eventitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TextView android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="15dp"
android:layout_weight="1" />
<TextView android:id="@+id/item_subtitle"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="13dp"
android:layout_weight="1" />
</LinearLayout>
logcat的
08-18 14:11:00.112: ERROR/AndroidRuntime(294): Uncaught handler: thread main exiting due to uncaught exception
08-18 14:11:00.142: ERROR/AndroidRuntime(294): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ctg/com.ctg.EventsActivity}: java.lang.NullPointerException
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.widget.TabHost.setCurrentTab(TabHost.java:320)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.View.performClick(View.java:2364)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.View.onTouchEvent(View.java:4179)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.View.dispatchTouchEvent(View.java:3709)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.os.Looper.loop(Looper.java:123)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at java.lang.reflect.Method.invoke(Method.java:521)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at dalvik.system.NativeStart.main(Native Method)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): Caused by: java.lang.NullPointerException
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at com.ctg.EventsActivity.onCreate(EventsActivity.java:64)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-18 14:11:00.142: ERROR/AndroidRuntime(294): ... 30 more
答案 0 :(得分:1)
1.1。这一切都取决于您的代码以及您如何获取信息。如果从数据库中获取信息,最容易使用的适配器将在我的操作中使用CursorAdapter,如果从解析中得到它,我将使用ArrayAdapter。 我会创建自己的自定义适配器,扩展ArrayAdapter。 Here is an example.还有很多其他的例子。
2:你的崩溃。查看EventsActivity中的第64行并在此处发布。否则,请包含完整的源代码以及所有导入和包,以便我们查看。