title: Unable to call methods of a custom view (canvasview) from Activity or class
body: <p>I'm unable to call methods of a custom view ("canvasview") from the Activity that sets the layout including the view. I can't even call canvasview's "getters" from the activity.</p>
  
  <p>Also, I'm passing the view to a custom class (that does not extend Activity), and I can't call canvasview's methods also from my custom class.</p>
  
  <p>I'm not sure what I'm doing wrong...</p>
  
  <p><strong>GameActivity.java:</strong></p>
  
  <pre><code>public class GameActivity extends Activity implements OnClickListener
  {
  
      private View canvasview;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.game_layout);
  
          canvasview = (View) findViewById(R.id.canvasview);
  
          // Eclipse displays ERROR con those 2 method calls:
          int w = canvasview.get_canvaswidth();
          int h = canvasview.get_canvasheight();
      (...)
  </code></pre>
  
  <p><strong>game_layout.xml:</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/LinearLayout2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".GameActivity" &gt;
  
      (...)
  
      &lt;com.example.test.CanvasView
          android:id="@+id/canvasview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>CanvasView.java:</strong></p>
  
  <pre><code>public class CanvasView extends View
  {
      private Context context;
      private View view;
      private int canvaswidth;
      private int canvasheight;
  
      public CanvasView(Context context, AttributeSet attrs)
      {
          super(context, attrs);
          this.context = context;
          this.view = this;
      }
  
  
      @Override
      protected void onSizeChanged(int width, int height, 
                                   int old_width, int old_height)
      {
          this.canvaswidth = width;
          this.canvasheight = height;
          super.onSizeChanged(width, height, old_width, old_height);
      }
  
      public int get_canvaswidth()
      {
          return this.canvaswidth;
      }
  
      public int get_canvasheight()
      {
          return this.canvasheight;
      }    
  </code></pre>
  
  <p>I'm quite confused with this :?</p>
  
  <p>I also have another class (it does not extend "Activity") that receives in the constructor a reference to canvasview and is also unable to "resolve" it :?</p>
  
  <p>Thanks, sorry if the question is too obvious, I'm starting with Java and those kind of things are quite confusing to me ...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>While at bed (03:00AM), thinking about it, I've noticed that Eclipse marks the line as an error because the View object does not really have the method get_canvaswidth(). Only the child "CanvasView" method has it. Thus, my problem can be solved with <strong>upcast</strong>: </p>
  
  <pre><code>int w = ((CanvasView) canvasview).get_canvaswidth();
  </code></pre>
  
  <p>I mean that I receive a view as parameter, but as I now it's really a view child, I should be able to use upcast to call "child's" methods. Now eclipse does not generate errors <strong>but</strong> w and h always report 0 :-? . I've also tested of not using upcast as has been suggested in an answer, and sending and receiving CanvasView objects in the calls and I also get 0 for both :?</p>