TabHost的Activity可以是ListActivity吗?

发布于 2024-11-29 23:18:51 字数 8245 浏览 0 评论 0原文

EventsActivity 是我的选项卡之一(3 个选项卡有 3 个活动)。

问题是:

  1. 这个Activity能以列表的形式显示吗?如果是,我应该使用哪个列表(SimpleListArrayList 等)?

  2. 我使用 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

EventsActivity is one of my tabs (3 tabs have 3 activities).

The question is:

  1. Can this Activity be displayed as a list? If yes, which list should I use (SimpleList, ArrayList, etc.)?

  2. I use an ArrayList HashMap but the stop unexpectedly occurs. How should I fix this?

I have some example in my code:

Class EventsActivity extends ListActivity. This is one of my tabs.

Class EventDataSet. Getter and setter methods.

Class EventXMLHandler. XML Handler.

Class Convertor. Converts date and time.

Layout Folder:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2024-12-06 23:18:51
  1. 是的,可以。

1.1.这完全取决于您的代码以及您获取信息的方式。如果你从数据库获取信息,最容易使用的适配器是 CursorAdapter,如果你像你一样通过解析获取信息,我会使用 ArrayAdapter。
我将创建自己的自定义适配器来扩展 ArrayAdapter。 这是一个示例。 有还有很多其他例子。

2:你的崩溃。查看 EventsActivity 中的第 64 行并将其发布到此处。否则,请包含您的完整源代码以及所有导入和包,以便我们查看。

  1. Yes it can.

1.1. It all depends on your code and how you get the information. The easiest Adapter to use if you get the information from a database would in my oppinion be CursorAdapter and if you get it from parsing like you do I would use an ArrayAdapter.
I would create my own custom adapter which extends ArrayAdapter. Here is an example. There are loads of other examples out there.

2: Your crash. Take a look at line 64 in your EventsActivity and post it here. Otherwise, please include your complete source code with all the imports and packages so that we can take a look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文