Search code examples
androidviewfindviewbyid

Android - findViewById returns null


I'm following an example from a book and I can't understand why findViewById returns null.

This is my activity:

package it.mt.compass;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class CompassActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        CompassView cv = (CompassView)this.findViewById(R.id.compassView1);

        // this crashes the application
        //cv.setBearing(45);

        // some debug code
        Toast test_result;

        if(cv == null) {
            test_result = Toast.makeText(this, "1", Toast.LENGTH_SHORT);
            test_result.show();
        }
        else {
            test_result = Toast.makeText(this, "0", Toast.LENGTH_SHORT);
            test_result.show();
        }

        // it shows 1
    }
}

and this is the res/layout/main.xml

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

    <it.mt.compass.CompassView 
        android:id="@+id/compassView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
</LinearLayout>

Already cleaned (as suggested in other similar topics; what does "Clean" do?) the project with no luck.

Many thanks in advance. Mirko

As requested, the constructors' code:

// Constructors


public CompassView(Context context) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet attrs) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet ats, int defaultStyle) {
        super(context);
        initCompassView();
    }

That's the correct version (the problem was I didn't passed the parameters correctly to the superclass constructor):

// Constructors


public CompassView(Context context) {
        super(context);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCompassView();
    }

    public CompassView(Context context, AttributeSet ats, int defaultStyle) {
        super(context, ats, defaultStyle);
        initCompassView();
    }

Solution

  • CompassView constructor implementation is incorrect. You're not passing the attributes to superclass and hence the id is lost.

    Change here the superclass constructor invocation

    public CompassView(Context context, AttributeSet attrs) {
        super(context);
    

    to super(context, attrs);

    and

    public CompassView(Context context, AttributeSet ats, int defaultStyle) {
        super(context);
    

    to super(context, attrs, defaultStyle); if the superclass has a ctor that accepts three args. Otherwise just use super(context, attrs). Oh, and rename the arg name from ats, even though the name doesn't matter.