Question

i have a problem with my listview. I have a list whit headers.

List: artistas_list header: artistas_list_s

In the header i wan put a letter ( A , B, C, D, .... )

All works fine except when scroll the list. When scrolling appear a null pointer exception in this line: tt.setText(o.nombre);

It's strange because at the firs time works fine.

Ok... then y paste the code of the getView

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.artistas_list, null);
        }

        Object content = null;

        Artista o = artistas.get(position);

        try {
            URL url = new URL(o.foto);
            content = url.getContent();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        InputStream is = (InputStream) content;
        Drawable image = Drawable.createFromStream(is, "src");

        TextView tt = (TextView) v.findViewById(R.id.ArtistTopText);
        TextView nid = (TextView) v.findViewById(R.id.txtnid);
        ImageView it = (ImageView) v.findViewById(R.id.imageView1);

        tt.setText(o.nombre);
        nid.setText(o.nid);
        it.setImageDrawable(image);


        String letra= o.nombre.substring(0, 1);


        if(!letra.equals(letraA)){      
            LayoutInflater vo = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vo.inflate(R.layout.artistas_list_s, null);                 
        }
        letraA = letra;
        return v;
    }

Ok, thx for read and try help !!!

LogCat:

 08-28 09:45:24.283: E/AndroidRuntime(512): FATAL EXCEPTION: main
    08-28 09:45:24.283: E/AndroidRuntime(512): java.lang.NullPointerException
    08-28 09:45:24.283: E/AndroidRuntime(512):  at es.okisam.comedy.artistas.ArtistasActivity$ListAdaptor.getView(ArtistasActivity.java:95)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.AbsListView.obtainView(AbsListView.java:1409)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.ListView.fillDown(ListView.java:670)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.ListView.fillGap(ListView.java:641)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3399)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:2233)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.widget.ListView.onTouchEvent(ListView.java:3446)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.View.dispatchTouchEvent(View.java:3885)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.os.Looper.loop(Looper.java:130)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at java.lang.reflect.Method.invoke(Method.java:507)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    08-28 09:45:24.283: E/AndroidRuntime(512):  at dalvik.system.NativeStart.main(Native Method)

artistas_list_s

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textSeparator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:gravity="center"
        android:text="text"
        android:textColor="#FFFFFFFF"
        android:visibility="visible" />

</LinearLayout>    

artistas_list

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/fondo"
    android:baselineAligned="false"
    android:gravity="center_vertical|top"
    android:orientation="horizontal"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:adjustViewBounds="true"
        android:background="@drawable/border_images"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/ArtistTopText"
        android:layout_width="190dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/txtnid"a
        android:layout_toRightOf="@+id/imageView1"
        android:layout_weight="0.02"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#000" />

    <TextView
        android:id="@+id/txtnid"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:src="@drawable/estrella_destacados"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="24dp"
        android:layout_weight="1"
        android:src="@drawable/flecha" />

</RelativeLayout>    

Update with logcat and layouts. Remmember... nullpointer appear when scroll... :S

Was it helpful?

Solution

you are not supposed to do

content = url.getContent();

in the getview, to begin with, as it is ran in the UI thread.

Then,

InputStream is = (InputStream) content;

could be null since it is outside the try {} catch {} block

finally, given we have no stacktrace or log whatsoever, it may be pretty much anything in your code (views not existing in your layout, object having null fields ...)

Also, your design is weird. If your element does not begin by the same letter as the previous one, you don't display it, and instead only display its initial?

Edit

After looking closely at this, here is what happens in your code:

  1. you inflate artistas_list
  2. you populate it with a thing starting with a
  3. you do the same for a thing starting with b
  4. you inflate artistas_list_s because it does not start with a
  5. you later receive this as a convertView in getView
  6. you look for views that are not present in it (like ArtistTopText)

See this example http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/ for an example of what you seem to be trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top