[英]Why i am not able to load the Sub class in to the xml layout?
In My Application, My XML layout is like this: 在我的应用程序中,我的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>
Where MyView is the subclass of the FingerPaint Project. 其中MyView是FingerPaint项目的子类。
Now in Java File i am going to handle that resources as like below: 现在在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);
}
Now theProblem is I got the other view but not that sub class view. 现在问题是我得到了另一个视图,但是没有那个子类视图。
Edited: After doing above, I dont get success, then i have try another method: 编辑:完成上述操作后,我没有成功,然后尝试了另一种方法:
I have create the relative layout in to that layout and add the content view for that Relative layout but still it not works, and give me null pointer Exception: 我已经在该布局中创建了相对布局,并为该相对布局添加了内容视图,但仍然无法正常工作,并给了我空指针异常:
XML code for second technique is: 第二种技术的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>
And the Java Code for the implementation is: 实现的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);
}
So, doing above both i am not able to get the View of the sub class ? 因此,在以上两者中我都无法获得子类的视图? Where i am wrong ??
我哪里错了?? Please help me for this.
请帮我。 .
。 .
。 Thanks.
谢谢。
如果您尝试在列出的布局中包含其他布局(MyView),请查看“包含和合并”: http : //developer.android.com/resources/articles/layout-tricks-merge。 html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.