2011-12-01 81 views
0

在我的申請,我的XML佈局是這樣的:爲什麼我無法將Sub類加載到xml佈局中?

<RelativeLayout 
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  

     <View 
      class="com.project.twsbi.FingerPaint$MyView" 
      android:id="@+id/image"  
      android:layout_width="fill_parent"   
      android:layout_height="fill_parent" 
      /> 
     </RelativeLayout> 

哪裏MyView的是FingerPaint項目的子類。

現在Java文件我要去處理資源爲象下面這樣:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(new MyView(this)); // Edited 
    setContentView(R.layout.main); 
    display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 


    // ste resources to View of Paint 
    View view = (View)findViewById(R.id.image); 

    view = new MyView(this); 
    } 

現在theProblem是我得到了另一種觀點,但不是子類視圖。

編輯: 上述工作後,我沒有得到成功,那我也嘗試另一種方法:

我在該佈局創建相對佈局和添加內容視圖,而且,相對佈局,但它仍然不工作,並給我空指針異常:對於第二種技術

XML代碼:

<RelativeLayout 
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  

     <RelativeLayout 
      android:id="@+id/drawingLayout"  
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent"> 

     </RelativeLayout> 
</RelativeLayout> 

和Java代碼的實現是:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(new MyView(this)); // Edited 
    setContentView(R.layout.main); 

    drawingLayout = (RelativeLayout)findViewById(R.id.drawingLayout); 


    // ste resources to View of Paint 
    //view = (View)findViewById(R.id.image); 

    myView = new MyView(this); 
    RelativeLayout innerLayout = new RelativeLayout(getApplicationContext()); 
    innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
    //drawingLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
    LayoutParams lp = drawingLayout.getLayoutParams(); // got nullPointer exception here 
    //innerLayout.addView(view); 
    //drawingLayout.addView(innerLayout); 
    addContentView(myView, lp); 
    } 

因此,上述兩個我都無法獲得子類的視圖? 我在哪裏錯了? 請幫我這個。 。 。 謝謝。

回答

相關問題