NullPointerException 自定义列表视图适配器
Posted
技术标签:
【中文标题】NullPointerException 自定义列表视图适配器【英文标题】:NullPointerException custom listview adapter 【发布时间】:2012-02-21 21:21:25 【问题描述】:你好 *** 社区,
我在扩展 BaseAdapter 的自定义适配器类的 getView 方法中获得了 NPE。我希望你能帮助我。
这是我的 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_
android:layout_
android:orientation="horizontal"
>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
android:padding="4dip"
>
<TextView
android:id="@+id/date"
android:layout_
android:layout_
android:textSize="15dip"
android:gravity="left" android:paddingLeft="6dip"
android:layout_weight="1" />
<TextView
android:id="@+id/cat"
android:layout_
android:layout_
android:textSize="15dip"
android:gravity="left" android:paddingLeft="6dip"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center"
android:padding="4dip"
>
<TextView
android:id="@+id/value"
android:layout_
android:layout_
android:textSize="40dip"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/notelayout"
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="right"
android:layout_gravity="center"
android:padding="4dip"
>
<ImageView
android:id="@+id/note"
android:layout_
android:layout_
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);
【问题讨论】:
你的代码好像没问题,尝试清理你的项目,删除 gen.R 并再次运行它 删除 gen.R 没有帮助.. 【参考方案1】:去掉getView()
中convertView==null的检查条件直接编写Layout inflating代码,不包含在if
中【讨论】:
以上是关于NullPointerException 自定义列表视图适配器的主要内容,如果未能解决你的问题,请参考以下文章
从自定义视图访问 RelativeLayout 时出现 NullPointerException
使用自定义 View 或 SurfaceView 时如何避免 NullPointerException
在自定义 ListView 适配器中执行 findViewById() 时发生 NullPointerException
你好,遇到跟你一样的问题。 android 自定义控件NullPointerException,请问是怎么解决的。