NullPointerException自定义列表视图适配器

时间:2012-01-29 23:39:37

标签: android listview nullpointerexception baseadapter

Hello stackoverflow社区,

我在我的自定义适配器类的getView方法中获得了一个NPE,它扩展了BaseAdapter。我希望你能帮助我。

这是我的getView方法:

@Override
public View getView(int pos, View convertView, ViewGroup viewGroup) {
    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater)context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row, null);
    }

    TextView datetv = (TextView) convertView.findViewById(R.id.date);
    TextView valuetv = (TextView) convertView.findViewById(R.id.value);
    TextView cattv = (TextView) convertView.findViewById(R.id.cat);

    if(values.get(pos)>VALUE_MIN&&values.get(pos)<VALUE_MAX){
        datetv.setTextColor(context.getResources().getColor(R.color.Good));
        valuetv.setTextColor(context.getResources().getColor(R.color.Good));
        cattv.setTextColor(context.getResources().getColor(R.color.Good));
    }
    else{
        datetv.setTextColor(context.getResources().getColor(R.color.Bad));
        valuetv.setTextColor(context.getResources().getColor(R.color.Bad));
        cattv.setTextColor(context.getResources().getColor(R.color.Bad));
    }

    datetv.setText(dates.get(pos));
    valuetv.setText(Double.toString(values.get(pos)));
    cattv.setText(cats.get(pos));

    LinearLayout l = (LinearLayout) convertView.findViewById(R.id.notelayout);
    l.setFocusableInTouchMode(false);
    l.setFocusable(false);
    l.setOnClickListener(this);
    l.setId(pos);

    return convertView;
}

这是row.xml:

    <?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"
  android:orientation="horizontal"

>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="140dip"
    android:layout_height="wrap_content" 
    android:padding="4dip"
    >
<TextView 
android:id="@+id/date" 
android:layout_width="fill_parent" 
android:layout_height="24dip" 
android:textSize="15dip"
android:gravity="left"  android:paddingLeft="6dip" 
android:layout_weight="1" />
<TextView 
android:id="@+id/cat" 
android:layout_width="fill_parent" 
android:layout_height="24dip" 
android:textSize="15dip"
android:gravity="left"  android:paddingLeft="6dip"  
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:gravity="center"
    android:padding="4dip"
    >
    <TextView 
android:id="@+id/value" 
android:layout_width="fill_parent" 
android:layout_height="48dip" 
android:textSize="40dip"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
    android:id="@+id/notelayout"
    android:orientation="vertical"
    android:layout_width="50dip"
    android:layout_height="wrap_content" 
    android:gravity="right"
    android:layout_gravity="center"
    android:padding="4dip"
    >
<ImageView
android:id="@+id/note" 
android:layout_width="wrap_content" 
android:layout_height="48dip" 
android:gravity="center"
android:src="@drawable/papiertje"
android:scaleType="centerInside"/>
</LinearLayout>
</LinearLayout>

这就是我得到的错误:

01-30 00:34:31.772: ERROR/AndroidRuntime(497): FATAL EXCEPTION: main
01-30 00:34:31.772: ERROR/AndroidRuntime(497): java.lang.NullPointerException
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at com.yellos.android.glucose.ListViewAdapter.getView(ListViewAdapter.java:85)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.ListView.onMeasure(ListView.java:1109)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.View.measure(View.java:8171)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:696)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:306)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.View.measure(View.java:8171)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.View.measure(View.java:8171)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.View.measure(View.java:8171)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.View.measure(View.java:8171)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.os.Looper.loop(Looper.java:123)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at java.lang.reflect.Method.invoke(Method.java:521)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-30 00:34:31.772: ERROR/AndroidRuntime(497):     at dalvik.system.NativeStart.main(Native Method)

第85行是l.setFocusableInTouchMode(false);

1 个答案:

答案 0 :(得分:1)

getView()

中删除 convertView == null 的检查条件

直接编写布局充气代码而不包含在