我需要擴展一個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)
這段代碼就是一個例子。 我有一個擴展ListView與其他事物的許多覆蓋。 – rodrigocorsi 2012-03-15 20:21:12