(kotlin异常) java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses

Encountered in development:

Android java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses

 After carefully to find the corresponding ID, the corresponding ID and confirmation pages and there is no problem, global search found the layout file with the same name in a different module, but the individual accounts, it does not normally have any problems.

 According to a thorough search and investigation, from the following about this issue:

android is how to find the control based on the id

First of all, before you call findViewById, you must be set up in activity in the setContentView, or overloaded onCreatedView methods Fragment, for findViewById, he can only be used in the view or activity, for the view, you were the root node traversal is the corresponding view, for the activity, you use the corresponding root that you initialize layout setContentView

When you call findViewById is, android to compare itself whether the id, it returns themselves, not the judge himself whether ViewGroup, if the child view is then traversed, otherwise it returns null, when traversed, from top to bottom in accordance with order to name traversal, just find a node id for the id search, view this node represents a return, say you have two identical layout id of view, it is inevitable that a return to the forefront of

Why is there such a situation

First, we look LOG

java.lang.NoSuchFieldError: No static field tabTexts of type I in class Lcom/xxx/R$id; or its superclasses (declaration of 'com.xxx.R$id' appears in /data/data/com.sss/files/instant-run/dex/slice-slice_1-classes.dex)

 This is the key issue here to tell we did not find tabTexts field (ID), strange, I have not touched, why suddenly such a problem. In this respect we can start to find the problem.

Solve the problem

Speaking of the above, it would traverse to find the appropriate layout when calling findViewById, if not null is returned. The same NoSuchFieldError is an exception in the Java reflection, it said it could not find the required fields through reflection. Were here, we consider it is time not loaded, not loaded with the appropriate layout. Can not find tabTexts lead to the ID of the control. So overall layout of the name search, finally found the drawbacks here. Let's look at search results:

Two with the same name but different ID, the problem really occurs here.

Project when loading layout, the default loaded 0x7f0d01c6 of ID, cause can not be found tabTexts.

 

Solution:
            custom_tab (is being given layout) for a name. Android avoid loading wrong layout.

 

by: yzl

Guess you like

Origin www.cnblogs.com/widgetbox/p/12125744.html