Home > Android Development > findViewById return null !!

findViewById return null !!

Trying to cast my view always returned null:

m_View = (MyCustomView)findViewById(R.id.View1);

After A googling a bit I’ve found my problem:

MyCustomViewconstructor didn’t contain “AttributeSet attrs”.

Modifying it fixed the problem.

So two important things to remember:

1. Must call setContentView before findViewById
2. Custom view constructor must receive the following parameters: Context context, AttributeSet attrs

Good luck! 🙂

Categories: Android Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: