Unable to find components using the findViewById method in Android
I'm attempting to create an Activity and unfortunately every time I want to grab one of my XML components it gives me a RunTimeException (NullPointer).
Anytime I use code such as:
TextView tv = (TextView) findViewById(R.id.myView); //I get the exception
The same happens for any components I attempt to find with that method. I can't quite figure out why. I know it isn't due to the Activity not being in the Manifest because it's the only Activity in the test app I made. (The one set up by default).
Oddly I can still use setContentView(R.id.myView)
. It just doesn't seem to want to find anything when using the findViewById
method.
Info that might be of use:
- I am currently using NetBeans as my IDE.
- I have done multiple 'clean and builds' as was suggested in another question. Android -findViewById question
Has anyone run into this issue before? If so, what was the solution?
If need be, I can provide sample code o开发者_如何学Pythonf when this is happening.
Don't pass in a view ID to setContentView
, pass in a layout resource ID:
setContentView(R.layout.layout_name);
If you still have problems, post your layout file.
It is very sure that you R.java is not properly generated.
Delete R.Java in netbeans IDE and Re-build the project.
Hope it resolves your query.
精彩评论