简体   繁体   中英

Why i am not able to load the Sub class in to the xml layout?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM