0

我的列表视图有问题。我有一个带有标题的列表。

列表:artistas_list 标题:artistas_list_s

在标题中我想放一个字母(A,B,C,D,....)

除非滚动列表,否则一切正常。滚动时在这一行出现空指针异常:tt.setText(o.nombre);

这很奇怪,因为在第一时间工作正常。

好的...然后粘贴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;
    }

好的,谢谢阅读并尝试帮助!

日志猫:

 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)

艺术家名单

    <?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>    

艺术家列表

    <?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>    

使用 logcat 和布局进行更新。记住...滚动时出现空指针...:S

4

1 回答 1

1

你不应该这样做

content = url.getContent();

首先在 getview 中,因为它在 UI 线程中运行。

然后,

InputStream is = (InputStream) content;

可能为 null,因为它在 try {} catch {} 块之外

最后,鉴于我们没有任何堆栈跟踪或日志,它可能几乎是您代码中的任何内容(布局中不存在视图,对象具有空字段......)

另外,你的设计很奇怪。如果您的元素不是以与前一个相同的字母开头,则不显示它,而仅显示其首字母?

编辑

仔细查看后,您的代码中会发生以下情况:

  1. 你夸大了 Artistas_list
  2. 你用一个以 a 开头的东西填充它
  3. 你对以 b 开头的事情做同样的事情
  4. 你夸大了 Artistas_list_s 因为它不是以
  5. 您稍后会在 getView 中将其作为 convertView 收到
  6. 您寻找其中不存在的视图(如 ArtistTopText)

请参阅此示例http://w2davids.wordpress.com/android-sectioned-headers-in-listviews/以了解您似乎正在尝试做的事情。

于 2012-08-28T07:35:18.650 回答