setImageResource的java.lang.StackOverflowError

时间:2013-01-10 03:04:50

标签: java android

我的Android应用程序在初始化tabhost时崩溃。但是来自用户的崩溃日志相当奇怪!

大部分崩溃来自android 2.3.x,在android 4.x上运行良好。

我可以看到当我在“setImageResource”中遇到代码遇到无限递归时,我无法理解为什么。 代码:

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);   
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);  // crashed
    java.lang.StackOverflowError
at android.graphics.drawable.StateListDrawable$StateListState.<init>(StateListDrawable.java:257)
at android.graphics.drawable.StateListDrawable.<init>(StateListDrawable.java:303)
at android.graphics.drawable.StateListDrawable.<init>(StateListDrawable.java:70)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
at android.content.res.Resources.loadDrawable(Resources.java:1712)
at android.content.res.Resources.getDrawable(Resources.java:590)
at android.widget.ImageView.resolveUri(ImageView.java:564)
at android.widget.ImageView.setImageResource(ImageView.java:280)
at com.thankcreate.care.MainActivity.addTab(MainActivity.java:95)
at com.thankcreate.care.MainActivity.setTabs(MainActivity.java:78)
at com.thankcreate.care.MainActivity.onCreate(MainActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4277)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

这是由无限循环执行引起的。这可能是由drawable中用作图标的循环调用引起的。具有指定drawableId的Drawable可能正在调用第二个drawable,而第二个drawable则调用第一个drawable。

提供有关您正在使用的drawable的更多详细信息。这可能有助于提供更好的答案。