@NonNull XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyResint id, int assetCookie, @NonNull String type) throws NotFoundException { if (id != 0) { try { synchronized (mCachedXmlBlocks) { finalint[] cachedXmlBlockCookies = mCachedXmlBlockCookies; final String[] cachedXmlBlockFiles = mCachedXmlBlockFiles; final XmlBlock[] cachedXmlBlocks = mCachedXmlBlocks; // First see if this block is in our cache. finalintnum= cachedXmlBlockFiles.length; for (inti=0; i < num; i++) { if (cachedXmlBlockCookies[i] == assetCookie && cachedXmlBlockFiles[i] != null && cachedXmlBlockFiles[i].equals(file)) { return cachedXmlBlocks[i].newParser(); } }
// Not in the cache, create a new block and put it at // the next slot in the cache. finalXmlBlockblock= mAssets.openXmlBlockAsset(assetCookie, file); if (block != null) { finalintpos= (mLastCachedXmlBlockIndex + 1) % num; mLastCachedXmlBlockIndex = pos; finalXmlBlockoldBlock= cachedXmlBlocks[pos]; if (oldBlock != null) { oldBlock.close(); } cachedXmlBlockCookies[pos] = assetCookie; cachedXmlBlockFiles[pos] = file; cachedXmlBlocks[pos] = block; return block.newParser(); } } } catch (Exception e) { finalNotFoundExceptionrnf=newNotFoundException("File " + file + " from xml type " + type + " resource ID #0x" + Integer.toHexString(id)); rnf.initCause(e); throw rnf; } }
thrownewNotFoundException("File " + file + " from xml type " + type + " resource ID #0x" + Integer.toHexString(id)); }
加载指定布局文件的xml,生成XMLBlock:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*package*/final XmlBlock openXmlBlockAsset(int cookie, String fileName) throws IOException { synchronized (this) { if (!mOpen) { thrownewRuntimeException("Assetmanager has been closed"); } longxmlBlock= openXmlAssetNative(cookie, fileName); if (xmlBlock != 0) { XmlBlockres=newXmlBlock(this, xmlBlock); incRefsLocked(res.hashCode()); return res; } } thrownewFileNotFoundException("Asset XML file: " + fileName); } privatenativefinallongopenXmlAssetNative(int cookie, String fileName);
try { // Look for the root node. int type; while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty }
if (type != XmlPullParser.START_TAG) { thrownewInflateException(parser.getPositionDescription() + ": No start tag found!"); }
finalStringname= parser.getName();
if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } //如果是merge标签,查看是否是当前布局的父节点,不是的话抛出异常 if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { thrownewInflateException("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true"); }
rInflate(parser, root, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml //获取xml中的根节点 finalViewtemp= createViewFromTag(root, name, inflaterContext, attrs); } ... } } }