1. 程式人生 > >Finding all View-IDs of an Android UI

Finding all View-IDs of an Android UI

Recently I stumbled upon a NullPointerException caused by using findViewById(R.id.whatever) which returned null for this id. But the id was in the layout file. And I didn't change the layout programmatically. Furthermore other widgets were found without any problem. Hmm - what's going on?

To analyze the problem I thought about a way to find all views and its ids within my activity. Android makes this a bit more cumbersome than other UI-toolkits, but it's nevertheless possible. First you have to find the root view of your layout and from there you have to go down and iterate over all children of each

ViewGroup present.

But what was happening? Well... The root cause of the problem was that some files had changed and others not and the build did not clean all static references. The ids of the generated files had changed but being static had been inlined. The solution of course was dead simple: A simple recompile of all files solved the problem 🙂

Nevertheless: If you are wondering what is happening in your UI you might want to use this code. You simply have to download the HierarchicalViewIdViewer and call it's debugViewIds method, passing in a view you were able to retrieve using findViewById(R.id.whatever) and a logtag you want to use for logcat

.

The code logs the class names of all widgets and their ids:


V/grokkingandroid( 6427): traversing: TextView, id: 2131034112
V/grokkingandroid( 6427): traversing: LinearLayout, id: -1
V/grokkingandroid( 6427): traversing: LinearLayout, id: -1
V/grokkingandroid( 6427): traversing: ScrollView, id: -1
V/grokkingandroid( 6427): traversing: FrameLayout, id: 16908290
V/grokkingandroid( 6427): traversing: LinearLayout, id: -1
V/grokkingandroid( 6427): traversing: DecorView, id: -1
V/grokkingandroid( 6427): view: LinearLayout(-1)
V/grokkingandroid( 6427):  view: ViewStub(16909037)
V/grokkingandroid( 6427):  view: FrameLayout(-1)
V/grokkingandroid( 6427):   view: TextView(16908310)
V/grokkingandroid( 6427):  view: FrameLayout(16908290)
V/grokkingandroid( 6427):   view: ScrollView(-1)
V/grokkingandroid( 6427):    view: LinearLayout(-1)
V/grokkingandroid( 6427):     view: LinearLayout(-1)
V/grokkingandroid( 6427):      view: TextView(2131034112)
V/grokkingandroid( 6427):     view: LinearLayout(-1)
V/grokkingandroid( 6427):      view: Button(-1)
V/grokkingandroid( 6427):      view: Button(-1)

As you can see the identation makes it easy to identify hierarchies. Those "traversing" lines at the beginning of the log show you the way from the starting view to the very root of the hierarchy. From there on the method goes downwards and shows you all elements of the hierarchy.

Not all elements have an id value. Those that have none show a "-1" for the id. Others show their id value in hexadezimal. Alas you cannot get the name of the id you used in the xml file. When transferring the xml files to the R file, the Android tools transform them into a unique number and there is no way to get the name from this number.

相關推薦

Finding all View-IDs of an Android UI

Recently I stumbled upon a NullPointerException caused by using findViewById(R.id.whatever) which returned null for t

Make an android custom view, publish and open source.

Publish your library:After finish pushing your project to GitHub, there is the last step to get done: publish it so that developers can just add dependency

“只能在UI主執行緒更新View”這件小事,android ui

轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992     ”只能在UI主執行緒中更新View“。     這句話很熟悉吧?     來來,哥們,看一下下面的例子 @Override protected void onCreate(

Android-UI-關於View-一種toolbar和ScrollView同時存在且軟鍵盤遮擋ScrollView中的Edittext的解決辦法

問題描述 前段時間在忙工作室的招新app的時候遇到了一種難纏的情況,描述一下出問題的佈局: <RelativeLayout ...> <ImageView.../> <!--這三個控制元件可以可以看成一個toolbar

巧用Drawable 實現Android UI 元素間距效果

purple 固定 展示 .com otto 技巧 log contain dev 源文地址: 巧用Drawable 實現Android UI 元素間距效果 在大部分的移動UI或者Web UI都是基於網格概念而設計的。這種網格一般都是有一些對其的方塊組成,然後

QT學習之forward declaration of 'struct Ui::xxx';invalid use of incomplete struct "Ui::Widget"

文件 exceptio 錯誤 執行 類名 nbsp dia dial truct 解決QT:forward declaration of ‘struct Ui::xxx‘;invalid use of incomplete struct "Ui::Widget" 等莫名奇妙

Android UI布局之TableLayout

you column true xmlns art parent 名稱 str demo 從字面上了解TableLayout是一種表格式的布局。這樣的布局會把包括的元素以行和列的形式進行排列。表格的列數為每一行的最大列數。當然表格裏邊的單元格是能夠為空的。 實例:La

[LeetCode] 448.Find All Numbers Disappeared in an Array

return htm put lis inpu pear rand ati n) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; color: #4e9072 } p.p2 { margin: 0.0

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

ret i++ value reverse alua style 執行 掃描 span leetcode 逆波蘭式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Va

448. Find All Numbers Disappeared in an Array

number run extra red ive runtime ger blog array Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear t

Android UI開發神兵利器之Icon

mod ng- 介紹 water rac icons mark .com des 好的設計離不開Icon話不多。介紹2個國外的站點,一個用來找Icon,一個用來搞頁面設計http://dryicons.com/free-icons/http://www.webdesig

[Array]448. Find All Numbers Disappeared in an Array

isa where xtra style 基本思想 我們 tput hat ice Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a

Find All Numbers Disappeared in an Array

arr elf blog bject appear 列表生成式 all 但是 col     這道題為簡單題   題目:        思路:     這道題其實很簡單,但是我之前做出來總是超時,於是就優化了一下代碼,創建一個列表a,長度為len(nums)+1初始化為

[GeeksForGeeks] Remove all half nodes of a given binary tree

return all con ref side 技術 http ons != Given A binary Tree, how do you remove all the half nodes (which has only one child)? Note leaves

刪除過期備份時報no channel allocated for maintenance(of an appropriate type)

刪除過期備份時報no channel allocated for maintenance(of an appropriate type)問題描述:AIX 平臺Oracle 10G,今天發現數據庫歸檔空間滿了,就通過crosscheck 後,執行delete force obsolete時報no channel

[LeetCode] Degree of an Array

重復 his out pub this lock put second 可能 Given a non-empty array of non-negative integers nums, the degree of this array is defined as the

[LeetCode] Degree of an Array 數組的度

same have this oss ret ear test nat cau Given a non-empty array of non-negative integers nums, the degree of this array is defined

leetCode-Find All Numbers Disappeared in an Array

mark extra do it false strong scrip xtra view style Description: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), som

634. Find the Derangement of An Array

elements all cto ould n) ret size appears num In combinatorial mathematics, a derangement is a permutation of the elements of a set, such

poj3358 Period of an Infinite Binary Expansion

href sum using clas 我們 dot 現在 www. blog Period of an Infinite Binary Expansion     題目大意:給你一個分數,求這個分數二進制表示下從第幾位開始循環,並求出最小循環節長度。     註釋:i