我需要扩展 ListView来创建自定义组件。 我想把搜索按钮放在左下角浮动。 当我在onMessure中运行生成错误时。
此代码是一个片段,原始代码还有许多其他内容。
我尝试将ViewGroup父级更改为null,但是我收到了同样的错误。
mPainelView = inflater.inflate(R.layout.longlist_painel, null);
自定义视图:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/longlist_painel_seek"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@android:drawable/ic_menu_search" /></FrameLayout>
扩展ListView:
public class List2 extends ListView {
private View mPainelView;
private int mPainelViewWidth;
private int mPainelViewHeight;
public List2(Context context) {
super(context);
createPainel();
}
public List2(Context context, AttributeSet attrs) {
super(context, attrs);
createPainel();
}
public List2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
createPainel();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mPainelView != null) {
measureChild(mPainelView, widthMeasureSpec, heightMeasureSpec);
mPainelViewWidth = mPainelView.getMeasuredWidth();
mPainelViewHeight = mPainelView.getMeasuredHeight();
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (mPainelView != null) {
mPainelView.layout(0, this.getBottom()-mPainelViewHeight, mPainelViewWidth, mPainelViewHeight);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
drawChild(canvas, mPainelView, getDrawingTime());
}
private void createPainel(){
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPainelView = inflater.inflate(R.layout.longlist_painel, this);
}
}
错误:
java.lang.NullPointerException
at android.view.ViewGroup.measureChild(ViewGroup.java:3098)
at com.rodrigo.comp.LongList2.onMeasure(List2.java:40)
at android.view.View.measure(View.java:8173)
答案 0 :(得分:0)
您不需要扩展ListView,您可以使用RelativeLayout将视图放在彼此之上:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:padding="10dp"
android:text="Search"
/>
</RelativeLayout>
答案 1 :(得分:0)
首先我的英语不是我的母语,我同时遇到同样的问题,所以我用Google搜索并找到你,当它调用“measurChild”时,nullpointer异常被抛出,我发现你的mPainelView' s layoutParams为null,因此异常抛出。
如果你将inflate方法更改为“inflater.inflate(R.layout.longlist_painel,yourlistview,false);”
你可以解决这个问题