skyfoot
skyfoot

Reputation: 20769

Android custom view causing force close

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?

Upvotes: 0

Views: 692

Answers (3)

GaryD
GaryD

Reputation: 128

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

Upvotes: 2

frayser
frayser

Reputation: 1772

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

Upvotes: 0

Asahi
Asahi

Reputation: 13506

Check constructors that your HomeView implements:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 

Upvotes: 0

Related Questions