Assetmanager关闭后(action_menu_layout.xml)NotFoundException

2016年03月24日 Android 5条评论 阅读6916次

Assetmanager关闭后(action_menu_layout.xml)NotFoundException

在论坛上看到有人问到一个json如何去解析,我便在自己IDE上新建了一个简单的json工程,并且把提问者的json源作为txt文件放到了asset下,便于我去读取。

因为是测试工程,所以直接在MainActivity#onCreate()中,就直接去开始打开asset下的文件然后再去解析了。

有这样一个方法是从asset下读取文件的。

private String getJsonStrFromAsset(String path) {
    AssetManager am = null;
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;
    String jsonStr = "";
    try {
        am = getAssets();
        is = am.open(path);
        isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) {
            jsonStr += line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (am != null) {
            am.close();
        }
    }
    return jsonStr;
} 

另外还有一段代码是如何去解析这个jsonString的,因为和本文无关所以就不贴出来了。

(action_menu_layout.xml)NotFoundException异常日志

程序运行后,出现了crash。得到的日志如下

I/Process(15158): Sending signal. PID: 15158 SIG: 9
E/Trace(16282): error opening trace file: No such file or directory (2)
D/libEGL(16282): loaded /system/lib/egl/libEGL_mali.so
D/libEGL(16282): loaded /system/lib/egl/libGLESv1_CM_mali.so
D/libEGL(16282): loaded /system/lib/egl/libGLESv2_mali.so
D/(16282): Device driver API match
D/(16282): Device driver API version: 10
D/(16282): User space API version: 10 
D/(16282): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Fri Sep 28 10:42:56 KST 2012 
D/OpenGLRenderer(16282): Enabling debug mode 0
D/AndroidRuntime(16282): Shutting down VM
W/dalvikvm(16282): threadid=1: thread exiting with uncaught exception (group=0x416742a0)
E/AndroidRuntime(16282): FATAL EXCEPTION: main
E/AndroidRuntime(16282): android.content.res.Resources$NotFoundException: File res/layout/    action_menu_layout.xml from xml type layout resource ID #0x109001b
E/AndroidRuntime(16282):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2178)
E/AndroidRuntime(16282):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2133)
E/AndroidRuntime(16282):    at android.content.res.Resources.getLayout(Resources.java:865)
E/AndroidRuntime(16282):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
E/AndroidRuntime(16282):    at com.android.internal.view.menu.BaseMenuPresenter.getMenuView(    BaseMenuPresenter.java:74)
E/AndroidRuntime(16282):    at com.android.internal.view.menu.ActionMenuPresenter.getMenuView(    ActionMenuPresenter.java:222)
E/AndroidRuntime(16282):    at com.android.internal.widget.ActionBarView.setMenu(ActionBarView.java:469)
E/AndroidRuntime(16282):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.    java:435)
E/AndroidRuntime(16282):    at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(    PhoneWindow.java:813)
E/AndroidRuntime(16282):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3128)
E/AndroidRuntime(16282):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(16282):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(16282):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(16282):    at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(16282):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16282):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(16282):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.    java:1006)
E/AndroidRuntime(16282):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(16282):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(16282): Caused by: java.lang.RuntimeException: Assetmanager has been closed
E/AndroidRuntime(16282):    at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
E/AndroidRuntime(16282):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2160)
E/AndroidRuntime(16282):    ... 18 more 

首先让我关注的是android.content.res.Resources$NotFoundException: File res/layout/action_menu_layout.xml from xml type layout resource ID #0x109001b 这个异常信息。

第一感觉是,eclipse创建Android工程时会自动加上个menu,但我确实没有用到menu。在工程中全局搜了一下,也没有发现哪有用到action_menu_layout.xml这个资源文件的地方。

接下来就是debug调试。在调试的过程中,发现从asset读取json源是正常的,解析json也是完全正常的。又陷入了思索中。

去StackOverflow上搜搜试试,竟然还真遇到了一样的问题。感谢TWiStErRob大神的回答。揭开了问题的关键点。

Always check the root cause, i.e. the last exception in the chain: Caused by: java.lang.RuntimeException: Assetmanager has been closed

You probably mess around with resources in the Java code, look around, or maybe something is wrong with the Activity lifecycle, anything "hacky" static?

就是这几句回答,让我把问题的关注点从action_menu_layout.xml资源文件转移到了Assetmanager has been closed上。

接着就把getJsonStrFromAsset()中,am.close()注释,结果crash现象消失了。

得到的启发是,一定要把异常日志全读一遍,找到其中的要害。还剩下的遗憾就是至今也不明白为什么am.close() 会影响到action_menu_layout.xml资源文件找不到的NotFoundException异常。

先记录到这里,我在继续看下AssetManager和Resources这两个类的源码,看看能否找到答案。

分享本文至:

WRITTEN BY

avatar
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

5 条评论

  1. avatar rondamice

    <a href="http://www.deerwedding.com/dallas-mavericks-knit-hath">dallas mavericks knit</a> <a href="http://www.kofaxreseller.com/roshe-run-grau-gamma-gr%C3%BCn-trainersce">roshe run grau gamma gr眉n</a> <a href="http://www.susieqmovie.com/nike-dunk-low-sb-rosa-schuhek">nike dunk low sb rosa</a> <a href="http://www.woistmeinhund.com/flyknit-lunar-1-atomic-lyser%C3%B8d-scarpef">flyknit lunar 1 atomic lyser酶d</a> <a href="http://www.jennyreinio.com/herren-nike-free-run-2-gr%C3%BCn-orange-kurpesb">herren nike free run 2 gr眉n orange</a> <a href="http://www.ejectdicks.com/nike-internationalist-mid-negro-zapatillas-zapatosk">nike internationalist mid negro zapatillas</a>
    rondamice http://www.rondamice.com/

  2. avatar athetize

    <a href="http://www.rondamice.com/nike-air-max-2015-alle-rot-kit-scarpee">nike air max 2015 alle rot kit</a> <a href="http://www.neuroassocpc.com/kobe-icon-rojo-color-runningg">kobe icon rojo color</a> <a href="http://www.rohanchougule.com/nike-huarache-run-schwarz-and-wei%C3%9F-schuhei">nike huarache run schwarz and wei脽</a> <a href="http://www.tgpmusic.com/vintage-oklahoma-sooners-hat-hath">vintage oklahoma sooners hat</a> <a href="http://www.bigcbigcard.com/air-jordan-retro-17-plata-marr%C3%B3n-trainersce">air jordan retro 17 plata marr贸n</a> <a href="http://www.jbsbruxelles.com/nike-air-max-tn-rot-schwarz-schuhek">nike air max tn rot schwarz</a>
    athetize http://www.athetize.com/

  3. avatar roomwhiz

    <a href="http://www.adanastarpen.com/adidas-gazelle-boost-gituttio-scarpek">adidas gazelle boost gituttio</a> <a href="http://www.actconfident.com/nike-kobe-9-mid-bleu-ciel-oranger-chaussuresh">nike kobe 9 mid bleu ciel oranger</a> <a href="http://www.erzurumihl.com/prada-1m1132-wallets-in-black-53-pradah">prada 1m1132 wallets in black 53</a> <a href="http://www.candctowingil.com/syracuse-orange-15-carmelo-anthonyncaa-orange-jersey-nfli">syracuse orange 15 carmelo anthonyncaa orange jersey</a> <a href="http://www.onlysidekick.com/herre-nike-huarache-ultra-lyser%C3%B8d-r%C3%B8d-skol">herre nike huarache ultra lyser酶d r酶d</a> <a href="http://www.rmmassage.com/ralph-lauren-ombre-linen-shirt-poloh">ralph lauren ombre linen shirt</a>
    roomwhiz http://www.roomwhiz.com/

  4. avatar unirlisboa

    <a href="http://www.yeliseeva.com/air-jordan-retro-6-hommesthe-verte-rouge-chaussuresk">air jordan retro 6 hommesthe verte rouge</a> <a href="http://www.miembrojr.com/womens-the-north-face-denali-2-hoodie-northfaceh">womens the north face denali 2 hoodie</a> <a href="http://www.faridekhalaf.com/adidas-neo-canvas-grau-gr%C3%BCn-schuhel">adidas neo canvas grau gr眉n</a> <a href="http://www.rawbiofast.com/nike-lebron-12-rouge-vert-chaussuresq">nike lebron 12 rouge vert</a> <a href="http://www.mabarrel.com/adidas-neo-high-plata-blanco-zapatosl">adidas neo high plata blanco</a> <a href="http://www.almarsecurity.com/nike-kobe-9-low-kvinders-lilla-s%C3%B8lv-skoj">nike kobe 9 low kvinders lilla s酶lv</a>
    [url=http://www.unirlisboa.com/]unirlisboa[/url]

  5. avatar americanparkinsonla

    <a href="http://www.abetterwaymotors.net/nike-air-max-thea-noir-summit-blanc-schuhee">nike air max thea noir summit blanc</a> <a href="http://www.accessabath.net/zx-flux-new-media-schuhe-scarpeb">zx flux new media schuhe</a> <a href="http://www.acostadesign.net/adidas-ultra-boost-easy-mint-gr%C3%B8nn-schuhed">adidas ultra boost easy mint gr酶nn</a> <a href="http://www.agentorangelegacy.net/lebron-soldier-9-blau-and-wei%C3%9F-house-butyf">lebron soldier 9 blau and wei脽 house</a> <a href="http://www.aikenfamilypharmacy.net/air-jordan-1-retro-high-og-negro-toe-2016-obuve">air jordan 1 retro high og negro toe 2016</a> <a href="http://www.ajimi.net/air-jordan-son-of-mars-orange-gr%C3%A5-obuvg">air jordan son of mars orange gr氓</a>
    americanparkinsonla http://www.americanparkinsonla.net/

欢迎留言




用户登录

sitemap