2012-03-14 39 views
1

我需要擴展一個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

你不需要擴展的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> 
+0

這段代碼就是一個例子。 我有一個擴展ListView與其他事物的許多覆蓋。 – rodrigocorsi 2012-03-15 20:21:12

0

首先我的英語不是我的母語,我來到在同一時間同樣的問題,所以我GOOGLE了這一點,並找到U,當它援引了「measurChild」的空指針異常被倒掉,我發現你的mPainelView的layoutParams是null,所以拋出的異常。

如果您將膨脹方法更改爲「inflater.inflate(R.layout.longf_painel,yourlistview,false);」

你可以解決這個問題

相關問題