EventsActivity
是我的标签之一(3个标签有3个活动)。
问题是:
-
这个Activity可以显示为列表吗?如果是,我应该使用哪个列表(
SimpleList
,ArrayList
等)? -
我使用
ArrayList
HashMap
,但意外停止发生。我该如何解决这个问题?
我有一些例子在我的代码:
类
EventsActivity
扩展ListActivity
。这是我的一个标签。
EventDataSet
类。Getter和setter方法。
EventXMLHandler
类。XML处理程序。类转换器。转换日期和时间。
布局文件夹:
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
- 是的,它可以。
1.1。这完全取决于您的代码和获取信息的方式。如果你从数据库中获取信息,我认为最容易使用的适配器是CursorAdapter,如果你从解析中获得信息,我会使用ArrayAdapter。我将创建我自己的自定义适配器扩展ArrayAdapter。这里有一个例子。还有很多其他的例子。
2:你的车祸。看看你的EventsActivity中的第64行,并把它贴在这里。否则,请将您的完整源代码与所有导入和包一起包含,以便我们可以查看。