如何从以下代码解决java.lang.StackOverflowError?
我的代码是: -
public class main extends Activity implements OnItemClickListener {
private static final String ACTION_ADW_PICK_ICON="org.adw.launcher.icons.ACTION_PICK_ICON";
private boolean mPickerMode=false;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
setContentView(R.layout.main);
GridView g=(GridView) findViewById(R.id.icon_grid);
g.setNumColumns(GridView.AUTO_FIT);
g.setColumnWidth(iconSize);
g.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g.setVerticalSpacing(iconSize/3);
g.setOnItemClickListener(this);
IconsAdapter adapter=new IconsAdapter(this,iconSize);
g.setAdapter(adapter);
if(getIntent().getAction().equals(ACTION_ADW_PICK_ICON)){
mPickerMode=true;
}
}
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
if(mPickerMode){
Intent intent=new Intent();
Bitmap bitmap=null;
try{
bitmap=(Bitmap) adapter.getAdapter().getItem(position);
}catch (Exception e) {
}
if(bitmap!=null){
intent.putExtra("icon",bitmap);
setResult(RESULT_OK, intent);
}else{
setResult(RESULT_CANCELED, intent);
}
finish();
}
}
private class IconsAdapter extends BaseAdapter{
private Context mContext;
private int mIconSize;
public IconsAdapter(Context mContext, int iconsize) {
super();
this.mContext = mContext;
this.mIconSize = iconsize;
loadIcons();
}
@Override
public int getCount() {
return mThumbs.size();
}
@Override
public Object getItem(int position) {
//return mThumbIds[position];
return ((BitmapDrawable)mContext.getResources().getDrawable(mThumbs.get(position))).getBitmap();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbs.get(position));
return imageView;
}
private ArrayList<Integer> mThumbs;
////////////////////////////////////////////////
private void loadIcons() {
mThumbs = new ArrayList<Integer>();
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addIcons(resources, packageName, R.array.icon_pack);
}
private void addIcons(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
}
}
}
}
}
SatckTrace是: -
08-24 17:10:50.105: INFO/dalvikvm(633): threadid=1: stack overflow on call to Landroid/graphics/drawable/DrawableContainer$DrawableContainerState;.getChildCount:I
08-24 17:10:50.105: INFO/dalvikvm(633): method requires 8+20+0=28 bytes, fp is 0x418fa30c (12 left)
08-24 17:10:50.115: INFO/dalvikvm(633): expanding stack end (0x418fa300 to 0x418fa000)
08-24 17:10:50.115: INFO/dalvikvm(633): Shrank stack (to 0x418fa300, curFrame is 0x418fa4b0)
08-24 17:10:50.155: DEBUG/AndroidRuntime(633): Shutting down VM
08-24 17:10:50.155: WARN/dalvikvm(633): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-24 17:10:50.405: DEBUG/dalvikvm(633): GC_CONCURRENT freed 229K, 46% free 3170K/5831K, external 1903K/2179K, paused 7ms+6ms
08-24 17:10:50.485: ERROR/AndroidRuntime(633): FATAL EXCEPTION: main
08-24 17:10:50.485: ERROR/AndroidRuntime(633): java.lang.StackOverflowError
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable$StateListState.indexOfStateSet(StateListDrawable.java:274)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable$StateListState.access$000(StateListDrawable.java:253)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:95)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.<init>(StateListDrawable.java:306)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.<init>(StateListDrawable.java:70)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:749)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.getDrawable(Resources.java:581)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.Resources.loadDrawable(Resources.java:1694)
08-24 17:10:50.485: ERROR/AndroidRuntime(633): at android.content.res.R
08-24 17:10:50.655: WARN/ActivityManager(61): Force finishing activity org.adw.launcher/.Launcher
08-24 17:10:51.195: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{40673a60 org.adw.launcher/.Launcher}
08-24 17:10:51.585: WARN/ActivityManager(61): Launch timeout has expired, giving up wake lock!
08-24 17:10:52.615: WARN/InputManagerService(61): Got RemoteException sending setActive(false) notification to pid 589 uid 10039
08-24 17:10:53.015: INFO/Process(633): Sending signal. PID: 633 SIG: 9
08-24 17:10:53.035: INFO/ActivityManager(61): Process android.process.acore (pid 633) has died.
08-24 17:10:53.065: INFO/WindowManager(61): WIN DEATH: Window{406a12d0 org.adw.launcher/org.adw.launcher.Launcher paused=false}
08-24 17:10:57.465: DEBUG/dalvikvm(236): GC_EXPLICIT freed 13K, 51% free 2939K/5895K, external 716K/1038K, paused 80ms
08-24 17:11:02.158: WARN/ActivityManager(61): Activity destroy timeout for HistoryRecord{40673a60 org.adw.launcher/.Launcher}
答案 0 :(得分:5)
这看起来像某种无限循环错误,可能在一个或多个drawable中。你有一个引用第二个的drawable,第二个有一个循环引用回到第一个吗?这可能解释了你所看到的异常。如果没有看到你想要加载的drawable,就很难更精确。