简体   繁体   English

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

[英]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.

相关问题 为什么有时我无法加载广告? - Why i am not able to load Advertisement sometimes? 为什么我无法使用 ListView 中的按钮更改布局? - Why I am not able to change layout with button in a ListView? 为什么我无法创建android xml文件? - Why am I not able to create android xml file? 为什么我不能在一个类中实现一个类,它已经扩展了一个类 - why am i not able to implement a class in a class ,which already extends a class 在View类android中加载布局xml文件 - load a layout xml file in a View class android 我定义时为什么会收到ClassNotFoundException <shape> 在布局XML? - Why am I getting ClassNotFoundException when I define <shape> in layout XML? 为什么我无法在 android 工作室的 activity_main xml 文件中编码和编辑代码? - why I am not able to code and edit code in android studio in activity_main xml file? 为什么在我尝试将arraylist膨胀到布局上的应用程序加载时显示空白的白屏? - why it is showing blank white screen when i am load the app in which i am trying to inflate arraylist onto a layout? 为什么我在Android Studio中看不到布局预览,却看到一个“十字”符号并且无法添加任何元素? - Why can't I see the layout preview in Android Studio but instead I see a 'cross' sign and am not able to add any elements? 我无法从 android studio 中的子活动返回数据 - I am not able to return data from sub activity in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM