ListActivity shows empty and crashes after 2 minutes

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListActivity shows empty and crashes after 2 minutes

Postby Zonakusu » Sat Jun 20, 2009 6:27 pm

Hi!

I'm trying to create a Listview by using a ListActivity and a custom ArrayAdapter. But when I start the application it doesn't show anything, and it crashes after about 2 minutes.

I do know that the application is trying to show something because earlier I placed a debug TextView which was only visible when I stopped loading the Listview.

I used this article to help me, I ommited the progressDialogue though.
http://www.softwarepassion.com/?p=189

I also uploaded my project, maybe thats easier to check for bugs?
http://minna-media.com/files/Weer.zip

I hope you guys can help me! :)

Main activity: weer.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.minnamedia.weer;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. import android.app.ListActivity;
  11.  
  12. import android.content.Context;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.view.LayoutInflater;
  17.  
  18. import android.view.View;
  19.  
  20. import android.view.ViewGroup;
  21.  
  22. import android.widget.ArrayAdapter;
  23.  
  24. import android.widget.TextView;
  25.  
  26.  
  27.  
  28. public class weer extends ListActivity {
  29.  
  30.        
  31.  
  32.         private ArrayList<Location> locations;
  33.  
  34.         private LocationAdapter locationAdapter;
  35.  
  36.        
  37.  
  38.     /** Called when the activity is first created. */
  39.  
  40.     @Override
  41.  
  42.     public void onCreate(Bundle savedInstanceState) {
  43.  
  44.         super.onCreate(savedInstanceState);
  45.  
  46.         setContentView(R.layout.main);
  47.  
  48.  
  49.  
  50.         this.locations = new ArrayList<Location>();
  51.  
  52.        
  53.  
  54.         // Add the adapter
  55.  
  56.         this.locationAdapter = new LocationAdapter(this, R.layout.row, this.locations);        
  57.  
  58.         setListAdapter(this.locationAdapter);
  59.  
  60.        
  61.  
  62.         // Populate items
  63.  
  64.         this.loadLocations();
  65.  
  66.        
  67.  
  68.         // Update the list
  69.  
  70.         this.locationAdapter.notifyDataSetChanged();
  71.  
  72.         for(int i = 0; i < this.locations.size(); i++)
  73.  
  74.         {
  75.  
  76.                 this.locationAdapter.add(this.locations.get(i));        
  77.  
  78.         }
  79.  
  80.         this.locationAdapter.notifyDataSetChanged();
  81.  
  82.     }
  83.  
  84.    
  85.  
  86.     private void loadLocations()
  87.  
  88.     {
  89.  
  90.         Location testOne = new Location();
  91.  
  92.         testOne.setLocationName("Amsterdam, Netherlands");
  93.  
  94.         Location testTwo = new Location();
  95.  
  96.         testTwo.setLocationName("Haarlem, Netherlands");
  97.  
  98.        
  99.  
  100.         locations.add(testOne);
  101.  
  102.         locations.add(testTwo);
  103.  
  104.     }
  105.  
  106.    
  107.  
  108.     private class LocationAdapter extends ArrayAdapter<Location>
  109.  
  110.     {
  111.  
  112.         private ArrayList<Location> items;
  113.  
  114.  
  115.  
  116.                 public LocationAdapter(Context context, int textViewResourceId, ArrayList<Location> itemsArg)
  117.  
  118.                 {
  119.  
  120.                         super(context, textViewResourceId, itemsArg);
  121.  
  122.                         this.items = itemsArg;
  123.  
  124.                 }
  125.  
  126.                
  127.  
  128.                 @Override
  129.  
  130.                 public View getView(int position, View convertView, ViewGroup parent)
  131.  
  132.                 {
  133.  
  134.                         View rowView = convertView;
  135.  
  136.                        
  137.  
  138.                         // If no view selected, load our Row.XML
  139.  
  140.                         if(rowView == null)
  141.  
  142.                         {
  143.  
  144.                                 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  145.  
  146.                                 rowView = inflater.inflate(R.layout.row, null);
  147.  
  148.                         }
  149.  
  150.                        
  151.  
  152.                         // Get our information from the list
  153.  
  154.                         Location rowLocation = items.get(position);
  155.  
  156.                        
  157.  
  158.                         // Valid row? Fill it up
  159.  
  160.                         if(rowLocation != null)
  161.  
  162.                         {
  163.  
  164.                                 TextView locationName = (TextView)rowView.findViewById(R.id.locationName);
  165.  
  166.                                
  167.  
  168.                                 if(locationName != null)
  169.  
  170.                                         locationName.setText(rowLocation.getLocationName());
  171.  
  172.                         }
  173.  
  174.                        
  175.  
  176.                         return rowView;                
  177.  
  178.                 }      
  179.  
  180.     }
  181.  
  182. }
  183.  
  184.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4



Class for storing item information: location.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.minnamedia.weer;
  3.  
  4.  
  5.  
  6. public class Location
  7.  
  8. {
  9.  
  10.         private String locationName;
  11.  
  12.         private String locationString;
  13.  
  14.         private float longitude, latitude;
  15.  
  16.         private int locationType;
  17.  
  18.        
  19.  
  20.         public String getLocationName()
  21.  
  22.         {
  23.  
  24.                 return this.locationName;
  25.  
  26.         }
  27.  
  28.        
  29.  
  30.         public void setLocationName(String locationNameArg)
  31.  
  32.         {
  33.  
  34.                 this.locationName = locationNameArg;           
  35.  
  36.         }
  37.  
  38.        
  39.  
  40.         public String getLocationString()
  41.  
  42.         {
  43.  
  44.                 return this.locationString;
  45.  
  46.         }
  47.  
  48.        
  49.  
  50.         public void setLocationString(String locationStringArg)
  51.  
  52.         {
  53.  
  54.                 this.locationString = locationStringArg;               
  55.  
  56.         }
  57.  
  58.        
  59.  
  60.         public float getLongitude()
  61.  
  62.         {
  63.  
  64.                 return this.longitude;
  65.  
  66.         }
  67.  
  68.        
  69.  
  70.         public void setLongitude(float longitudeArg)
  71.  
  72.         {
  73.  
  74.                 this.longitude = longitudeArg;         
  75.  
  76.         }
  77.  
  78.        
  79.  
  80.         public float getLatitude()
  81.  
  82.         {
  83.  
  84.                 return this.latitude;
  85.  
  86.         }
  87.  
  88.        
  89.  
  90.         public void setLatitude(float latitudeArg)
  91.  
  92.         {
  93.  
  94.                 this.latitude = latitudeArg;           
  95.  
  96.         }
  97.  
  98.        
  99.  
  100.         public float getLocationType()
  101.  
  102.         {
  103.  
  104.                 return this.locationType;              
  105.  
  106.         }
  107.  
  108.        
  109.  
  110.         public void setLocationType(int locationTypeArg)
  111.  
  112.         {
  113.  
  114.                 this.locationType = locationTypeArg;
  115.  
  116.         }
  117.  
  118. }
  119.  
  120.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Main layout: main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.   android:orientation="vertical"
  7.  
  8.   android:layout_width="fill_parent"
  9.  
  10.   android:layout_height="fill_parent"
  11.  
  12.   >
  13.  
  14. <ListView
  15.  
  16.    android:id="@+id/android:list"
  17.  
  18.    android:layout_width="fill_parent"
  19.  
  20.    android:layout_height="fill_parent"
  21.  
  22.    />
  23.  
  24. </LinearLayout>
  25.  
  26.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Listview item layout: row.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:layout_width="fill_parent"
  7.  
  8.    android:layout_height="?android:attr/listPreferredItemHeight"
  9.  
  10.    android:padding="6dip">
  11.  
  12.         <TextView
  13.  
  14.             android:id="@+id/locationName"
  15.  
  16.             android:layout_width="fill_parent"
  17.  
  18.             android:layout_height="0dip"
  19.  
  20.             android:layout_weight="1"
  21.  
  22.             android:gravity="center_vertical"
  23.  
  24.         />
  25.  
  26. </LinearLayout>
  27.  
  28.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Zonakusu
Freshman
Freshman
 
Posts: 5
Joined: Sun Jun 07, 2009 7:56 pm

Top

Postby pentace » Sun Jun 21, 2009 1:11 pm

Just an fyi your project is empty
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby Zonakusu » Sun Jun 21, 2009 1:16 pm

Oh hell you're right! Fixed it, good one is UP now.

Anyone who spots the problem? Still havent fixed it...
Zonakusu
Freshman
Freshman
 
Posts: 5
Joined: Sun Jun 07, 2009 7:56 pm

Postby pentace » Sun Jun 21, 2009 1:17 pm

looking into it now i just coppied the code and added the extra files
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby pentace » Sun Jun 21, 2009 1:24 pm

just from what i see so far somethign is looping it chews up over 15 Megs right before it crashes
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby pentace » Sun Jun 21, 2009 1:30 pm

it is your for loop causing the issues it is infinitly looping
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Top

Postby Zonakusu » Sun Jun 21, 2009 2:32 pm

You are right! I removed the complete loop and it now works. Pretty weird though, don't understand why it would loop that much...

Also weird that the list already has the two populated listitems just by filling the ArrayList, i thought i had to manually add it to the adapter.
Zonakusu
Freshman
Freshman
 
Posts: 5
Joined: Sun Jun 07, 2009 7:56 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 4 guests