On Feb 22, 10:24 pm, Bilthon <[email protected]> wrote:
> If what you want is inflate a view from XML, you should try something like:
>
> ====================================================
> public class BoardActivity extends Activity{
>
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState)
>
>         Board boardView;
>         boardView = (Board) findViewById(R.id.board);
>         setContentView(boardView);
>  }
> ====================================================

That doesn't work.  findViewById will fail when you haven't yet
setContentView
Try one of these approaches.

===begin  custom_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyView xmlns:android="http://schemas.android.com/apk/res/
android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"/>
===end custom_view.xml

==snip from CustomViewActivity.java
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_view);

==end snip from CustomViewActivity.java

You can also embed your view in another Layout, just give it an id:
===container.xml===
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:orientation="vertical">
    <com.example.MyView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:id="@+id/custom_view/>
</LinearLayout>
===end container.xml===

==snip from ContainerActivity.java
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.container);
        MyView myCustomView = (MyView)findViewById(R.id.custom_view);

==end snip

Make sure the MyView class has a constructor that takes a Context and
an ApplicationSet and invokes super(Context, ApplicationSet)

Dale

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to