Question

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();
    }
Was it helpful?

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.

OTHER TIPS

In eclipse do:

  • Projects -> Clean.
  • Eefresh your app.
  • Run.

this will clear generated old R class.

I would try closing eclipse completely and then open it again. I've seen some really bizarre things like this happening. Another way you can try this is to just add a "textView" and try to do a findById on that and see if it's returning null. You could be loading the wrong xml view..

ie: your are loading a layout from one directory but it's actually loading a different view in a different directory with the same name...

add import it.mt.compass.R; and try another View, like a Image or TextView instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top