[英]Why i am not able to load the Sub class in to the xml layout?
在我的應用程序中,我的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 File中,我將如下處理資源:
@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);
}
現在問題是我得到了另一個視圖,但是沒有那個子類視圖。
編輯:完成上述操作后,我沒有成功,然后嘗試了另一種方法:
我已經在該布局中創建了相對布局,並為該相對布局添加了內容視圖,但仍然無法正常工作,並給了我空指針異常:
第二種技術的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);
}
因此,在以上兩者中我都無法獲得子類的視圖? 我哪里錯了?? 請幫我。 。 。 謝謝。
如果您嘗試在列出的布局中包含其他布局(MyView),請查看“包含和合並”: http : //developer.android.com/resources/articles/layout-tricks-merge。 html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.