views:

54

answers:

3

Hi,

I have a custom view in src > myproject.test > HomeView

In my main layout xml I have the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <myproject.test.HomeView
        android:id="@+id/home_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </myproject.test.HomeView>

</LinearLayout>

In the HomeActivity I have a call like this in the onCreate method.

setContentView(R.layout.main);
HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

The app force closed when the setContentView method is called. It seems that my main xml is not correct.

Any ideas?

+1  A: 

Do you mean its not getting to the

HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

and crashing on the line before it?

Check if your constructor is

HomeView(final Context context, final AttributeSet attrs){
     super(context, attrs);

and not

HomeView(final Context context){
     super(context);

you need the AttributeSet

GaryD
yes, if I comment out the HomeView setup line it still force closes
skyfoot
How do you initialize your home view? could you show some code, and the output from logcat
GaryD
Just out of intrest, is your layout also called main.xml ? and is it in the layout dir?
GaryD
it was the constructor. thanx
skyfoot
A: 

Check constructors that your HomeView implements:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 
Asahi
A: 
<LinearLayout xmlns:android = 
      http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"

None of my layouts have @+id. Maybe you should set the view to home_root. Check with you R.java for the name of the layout, or try

setContentView(R.layout.home_root);
Frayser