为什么我无法将子类加载到 xml 布局中?

发布于 2024-12-18 22:35:49 字数 2455 浏览 0 评论 0原文

在我的应用程序中,我的 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);
  }

现在的问题是我得到了另一个视图,但没有得到该子类视图。

编辑: 完成上述操作后,我没有成功,然后我尝试了另一种方法:

我已经在该布局中创建了相对布局,并为该相对布局添加了内容视图,但它仍然不起作用,并给我空指针异常:

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);
  }

那么,执行上述操作我无法获取子类的视图? 我哪里错了?? 请帮助我。 。 。 谢谢。

In My Application, My XML layout is like this:

<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.

Now in Java File i am going to handle that resources as like below:

 @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:

    <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:

 @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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-12-25 22:35:50

如果您尝试在列出的布局中包含不同的布局(MyView),请查看包含和合并:http://developer.android.com/resources/articles/layout-tricks-merge.html

If you're trying to include a different layout(MyView) inside of the layout you've listed, take a look at Include and Merge: http://developer.android.com/resources/articles/layout-tricks-merge.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文