Question

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
Était-ce utile?

La solution

  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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top