Unable to find components using the findViewById method in Android - 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 of 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.

Related

'this#ActivityName' is not captured error Android/Kotlin

I'm repairing my friend's code and got confused.
My friend wants to fetch entered text (in EditText). Seems easy, right? Well, it is but instead of user input, he gets this warning/error:
To be honest I'm not sure how to fix it. He is coding in Kotlin (Android 10).
Activity that contains EditText:
And XML:
This is how it looks when debugging:
The app started working just fine after running "File -> invalidate Cashes/Restart" option, I just don't understand where this warning came from and how to fix it because the error remained unchanged (even though the app works). Do you have an idea how to solve it?
All the best!
fyi lambda expression like setOnClickListener from kotlin is not debuggable, see here.
if you want to debug variables inside setOnClickListener you should use the normal one e.g. setOnClickListener(object: View.OnClickListener {..})
sometimes there will be problem in auto generated binding files, if so it will be solved after invalidate cache and restart ide. sometimes the warning/error show but the project and complied without errors. so no need to worry about that. for next time post the code as code not screen shots.
I understand that the question is regarding evaluating expression, but there is a way you can read variables from your debugger console, even if you're inside an anonymous callback. I found it helpful sometimes. Here are the steps:
First enter debugger mode inside of your anonymous callback,
In your debugger console, look at the right side for "Frames"
Within Frames under , you'll see stack of function execution first top one on the list is the latest:
Click on row(s) below the latest function, until you find an instance of your activity AddInformationActivity. You will see the activity instance on the right side window under Variables. Don't go as far as selecting functions browned out, because those are from internal libraries.
When you see you AddInformationActivity instance, you can expand it and see its variables.
Hope that helps!
It's not a beautiful way, but if you create a method like this:
private fun debug() {
println()
}
and add a breakpoint on the println() it'll capture the activity.
(Don't use TODO as it'll crash the app with a NotImplementedError once called.)
I have this method now in my code all the time to call it whenever I need it.
I know, that question is old, but I just stumbled over it and needed a way.

Some how I broke butterknife

Some how I managed to break something in my eclipse setup that is breaking butterknife for any new views I try to inject. The "old" views still seem to be fine.
This one works:
#InjectView(R.id.sign_out_button)
Button signOutButton;
this one does not
#InjectView(R.id.map_button)
Button mapButton;
When I add the new field I get this in the generated java file under .apt_generated
view = finder.findRequiredView(source, 0, "field 'mapButton'");
vs this for the "old" field
view = finder.findRequiredView(source, 2131165280, "field 'signOutButton' and method
note the 0 vs 2131165280
The error message I get is map_button cannot be resolved or is not a field
And indeed if I look under gen/my_package/R.java there is no entry for map_button, but there is one for sign_out_button.
I have tried cleaning the project and restarting the ide many times. Anyone have any ideas as to what might be wrong?
Solved
Turns out this was a really dumb mistake. I had not added the button to the xml file. Butterknife helps with the code injection but you still need to add the button to the layout file.

Cannot call 'SetContentView' on new activity - Android Mono

I've had a search for this problem but nothing seems to help me to solve this particular error I am getting.
I am writing my first Android app and am coming across a java.lang.RuntimeException whenever I call SetContentView on a new activity.
There is nothing in the logcat which helps (an activity idle timeout is all because it falters on the call).
My activity Login has a layout set during OnCreate which works fine, but any subsequent calls fall over. Here's some code ;)
[Activity(Label = "Usage")]
public class Usage : TabActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
//**FALLS OVER HERE**
SetContentView(Resource.Layout.Usage);
The Resource.Designer.cs has a record of my layout:
// aapt resource value: 0x7f030002
public const int Usage = 2130903042;
...and when I reference that layout by it's int value it falls back to the previous activity without hitting any breakpoints in the Usage activity.
Anyone got any thoughts or can point me in the direction of a similar post?
Legends!
UPDATE
I tried a whole stack of fixes I found on forums etc but nothing would fix this. I put the whole thing on the backburner while I worked on something else, came back to it and now it works...wish I could say what it was that made it work to help others out but I can't explain it! COULD have been an update to a new version of MonoDroid?
As stated in my comment to Stuart's answer, this problem appears to have resolved itself. I revisited the project all this time later and think that it might have just been a case of cleaning the project and rebuilding all. I have not had this problem since.
Sorry that this is not a detailed answer, I would suggest trying the ol' clean and rebuild.
I've recently had some issues when working in VS2010 where the resource ids are being not kept perfectly in-sync with the resource files and the java ids.
To resolve these, I generally find the quickest way is to add a new id to one of the layout xml files - this then causes a regeneration or the resources.cs file which then means the app works again.
If that doesn't help, then please post more info about what the message inside the RuntimeException is,
Although it's very late response, but someone who might be getting this sort of error. Wrap it with try-catch and it gives more details about the exception. I spent few hours before figuring out to do that

ImageView Id not being created in R file

Quite new to Android dev so there is a good chance I made a dumb mistake. But for some reason I cant reference a ImageView I created in a second Activity using the findViewById method. When I went and checked the 'R' file I could see that an Id was not created for all 3 ImageViews I am trying to reference that is in the same activity.
What could the possible reason be for this? Could it be something to do with data I put in the Manifest file? My java class? Because my syntax for the containing activity is 100%....
I'm suspecting is has something to do with the new activity/manifest. How can all the ImageViews in the same Activity not be referenced...?
Clean the project and try again. To clean you project Project -> Clean select your project and clean. Some time it happens while developing the project. I've faced many times.

Android DDMS exceptions?

As i'm new to android, i really couldn't understand the errors or execptions shown in the android DDMS,I'm using Eclipse ..
Can anyone please share some very frequent exceptions that appear in DDMS and how to handle them......
We should explain exceptions here for you? Maybe you should try to read the name of the exception... most of them are named understandable. And the very first thing you should do to learn about exceptions is to read their documentation: the very famous one for example: http://developer.android.com/reference/java/lang/NullPointerException.html
If that didnt help, try to search with google or here on StackOverflow...
If you opened any xml files, this error shoud occur. Close all xml files of your application.
I'm not certain but I'm also having the same problem. I believe it occurs when you try to call something that is out of context. For example if you tried to call
TextView myTextView= (TextView)findViewById(R.id.myTextView);
when you had set a contentView as something that did not contain 'myTextView'
Hope this helps!
I think this type of error occurred when you have edited the .xml file and rebuild the project.
If you have opened any xml files, save all the .xml files and Close all xml files of your application.
and run the project. this way i run my application when this type of error occurred.

Categories

Resources