我有这个简单的自定义 ListView:
public class ChildListView extends ListView {
private static final int ROW_HEIGHT = 43;
private Context mContext;
private int mRowCount = 0;
public ChildListView(Context context) {
super(context);
mContext = context;
}
public ChildListView(Context context, int numRows) {
super(context);
mContext = context;
mRowCount = numRows;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), mRowCount * Helper.dipToPx(mContext, ROW_HEIGHT));
}
}
我必须测量列表的高度,因为它是 ExpandableListView 的子项。
更多信息:在我向 ChildListView 的适配器添加另一个视图之前,它工作正常。现在它有两个视图,但我有这个例外:
10-26 14:33:43.781: E/AndroidRuntime(10138): FATAL EXCEPTION: main
10-26 14:33:43.781: E/AndroidRuntime(10138): java.lang.NullPointerException
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.measureScrapChild(ListView.java:1135)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.onMeasure(ListView.java:1104)
10-26 14:33:43.781: E/AndroidRuntime(10138): at package.views.ChildListView.onMeasure(ChildListView.java:38)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.measure(View.java:8526)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3228)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:701)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.measure(View.java:8526)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.setupChild(ListView.java:1869)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.makeAndAddView(ListView.java:1796)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.fillDown(ListView.java:670)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.fillSpecific(ListView.java:1350)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.ListView.layoutChildren(ListView.java:1624)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.layout(View.java:7277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.layout(View.java:7277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.layout(View.java:7277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.layout(View.java:7277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.View.layout(View.java:7277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.ViewRoot.performTraversals(ViewRoot.java:1203)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.view.ViewRoot.handleMessage(ViewRoot.java:1957)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.os.Looper.loop(Looper.java:150)
10-26 14:33:43.781: E/AndroidRuntime(10138): at android.app.ActivityThread.main(ActivityThread.java:4277)
10-26 14:33:43.781: E/AndroidRuntime(10138): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 14:33:43.781: E/AndroidRuntime(10138): at java.lang.reflect.Method.invoke(Method.java:507)
10-26 14:33:43.781: E/AndroidRuntime(10138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-26 14:33:43.781: E/AndroidRuntime(10138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-26 14:33:43.781: E/AndroidRuntime(10138): at dalvik.system.NativeStart.main(Native Method)
它被扔在这条线上:
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
感谢任何帮助!