Android studio nullpointer warnings/notice in code view - android

until now I couldn't find any answer that suits my problem/question.
In android studio it often shows a notice that e.g. findViewById may produce a NPE, althogh i know the item IS existing in the layout I use.
I also never got a NPE thrown at this locations at runtime, so why the warning/notice?
I just don't know how I have to react/handle to such warnings/infos in the code view? Are there any tips to avoid that messages (because for me it looks like valid and working code)? It's only the warnings in the code view, that makes me uncertain.
Here a snippet where a warning is shown on findViewByID.
//fetch predefined item layout
row = inflater.inflate(R.layout.view_searchresult_item,null);
//get table layout for inserting items
TableLayout itemtablelayout = (TableLayout)row.findViewById(R.id.resultitem_tablelayout);

Warning is not an Error. And the warning which you are talking about says "it may produce", don't say 'it must produce'. So choice is yours. Either add null check or not
So, If you are sure that findViewById in your code will never be cause of NPE, then don't add the null check.

Related

Fail to initialize resources in android app - then NullPointerException

So, I have this android application and even some users. As I have a crash report system, I can see when someone's app crashes and the cause.
It appears that, though rarely, the app crashes randomly with NullPointerException when it tries to change some attributes(rotation, text, etc..). I make sure everything is set first thing in the onViewCreated method(using Fragments) like this:
private TextView orientationView;
orientationView = this.getActivity().findViewById(R.id.orientationView);
Using this ^^ example, I then try to hide/show this view and get an exception as it appears to be null sometimes, which is what I struggle to figure out why.
orientationView.setVisibility(View.VISIBLE); // app crashes when orientationView is null
Being a newbie in android development, I am not sure if it is a good practice, but in some of the fragments, I set all the previously initialized resources to null in the onDestroyView method, but the one that crashes the most doesn't have this method implemented, which make me to believe that somehow the resources are just not found/initialized in some rare occasions and I fail to change them later with an exception.
Could someone help me figure this out :) (more description could be provided, if needed)

Is it always worth checking for null pointers?

Let's say we call a method on a variable fetched through findViewById():
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("Some text");
Android Studio automatically warns us that setText() may produce a NullPointerException if tv turns out to be null. If we however are certain that tv will never be null (unless something really wonky happens which should crash the app anyway), is it really worth encapsulating the method call within an if(tv != null){} statement? What if these two (or more) rows are executed very often? Can we gain any significant performance increase?
I personally don't think there is much point in checking these are null because you will know if they are present in your layout file or not, so I wouldn't bother.
However, I do sometimes use the following to prevent the warnings in Android Studio:
assert tv != null;
I don't know how this affects performance but I imagine it will have almost no difference.
Throwing or creating new exceptions will definitely consume more resources than what you might expect BUT it is always good to handle the exception where necessary rather than let the app crash. IF you are absolutely certain that the textview or whatever UI element ISpresent in the correct XML for your activity and/or the fragment then i don't think that you should use try catches at all..
Yes you can use asserts to avoid warning of Android Studio as mentioned above, and with respect to performance I don't think you will notice any huge performance variation. However, its always good to catch exceptions and log them as it can be really useful while debugging.
Actually, it can be null.
If you have complicated layout, and many ids, it can happen if you type id of other layouts.
This is only a warning. If your layout is simple to remember all ids or you sure it's never null, don't bother this warning.
This warning is helpful when you change an id in layout manually.
EX:
layout 1 has ids : text_view_1, button_1
layout 2 has ids : text_view_1, button_2
Activity 2 use layout 2 and findViewById(text_view_1)
If you change from text_view_1 to text_view_2 (MANUALLY by typing). No error happened, because id text_view_1 is still exits. But NullPointerException will occur when you run application.

How do I navigate through and understand the documentation in Android?

I got one piece of code to study and I was puzzled for a long time because I tried to make my own version of it and it broke then I tried commenting the original code step by step to see when it failed and it gave me a null pointer in a getView method after I commented the declaration of one variable it used. I wasn't seeing this method being called anywhere and searched a lot for an answer until I found this:
When is the getView() method of ListView called?
It esentially says that getView getts called whenever an item is passed to the adapter through the setAdapter method.
I look all over the View docs, Adapter docs, Inflater, etc and couldn't find any piece of information to tell me that this happened, not even the setAdapter method itself says anything about this behavior. Is this just a documentation error or is there some general guideline I'm not following correctly?
I think you are going in the Right direction, if you are breaking into the code and hitting road blocks. The best resource to Study API's for Android is android developer site itself
http://developer.android.com/reference/android/widget/Adapter.html
PLus the [android] tagged questions on StackOverflow.

How to find a code behind a view in hierarchyviewer

Android hierarchyviewer shows a ID for each view in the tree view.
The ID is a # sign followed by a number, e.g #4051d698
In android documentation the purpose of this number is explained as "pointer to view object".
Assuming one has the sources of a very big android project like AOSP.
How can one figure out what is the java source code behind the view by using this ID?
Is there a method I can invoke that tells me what is the R.java entry that is bound to this pointer?
How can one figure out what is the java source code behind the view by using this ID?
You can't, at least without a debugger. If you are used to C/C++ development, that hexadecimal value is roughly analogous to a pointer. Just because you have a pointer to a hunk of RAM in C/C++ does not mean that you can determine the source code behind the object resident at that pointer.
Now, it is possible that there is a way in a debugger to supply this hex value and the debugger will match that up to an object and source code. I am not an Eclipse expert, or an expert on another other IDE debuggers, to know whether or not there is a means to do this. However, even if can do this, it will probably only give you the source of the class of the object (e.g., if the View is a ListView, it might send you to the ListView source code), not the source code of what created the object.
Is there a method I can invoke that tells me what is the R.java entry that is bound to this pointer?
First, R.java is not "bound" to any pointers.
Second, the View in question may not have come from an inflated layout. It might have been created directly in Java code instead.
If the View has an "View object ID" (e.g., id/content), that can better help you find where it came from, as that will be an android:id value, possibly from your layout resources.

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 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.

Categories

Resources