Android + 列表 = 崩溃?

Android + List = Crash?

private List<MenuButton>menuButtonList=new ArrayList<MenuButton>();
MenuButton m2=new MenuButton(order,b , vertical, p,name);  
menuButtonList.add(m2);

当我将 "MenuButton" 添加到列表时,这三行导致发生在第 3 行的崩溃。我无法得到错误,因为我使用 bluestacks 并且我不知道如何使用它进行调试,有人知道为什么会这样吗?

编辑: 完整代码:

public class CreatorView extends View
{

Context c;

boolean quickfix=false;
boolean bin_isEmpty=true;
boolean iftrue=false;
boolean stopnow=false;
String arrowcheck="";
int screenw;
int screenh;
int pixelx;
int pixely;
int smallpixelx;
int smallpixely;
Point arrowsp;

Bitmap grass;
Bitmap dirt;
Bitmap dirt2;
Bitmap dirt3;
Bitmap cloud;
Bitmap bin_Empty;
Bitmap bin_Full;
Bitmap arrowno;
Bitmap arrown;
Bitmap arrowl;
Bitmap arrowr;
Bitmap arrowu;
Bitmap arrowd;
Bitmap arrowul;
Bitmap arrowur;
Bitmap arrowdl;
Bitmap arrowdr;
Bitmap grassSide;
Bitmap grassTop;
Bitmap orange;
Bitmap smallpixel;
Bitmap PixelDetect;


Map.Entry<Bitmap,Point>currentBlock;
Map.Entry<Bitmap,Point>lastBlock;
Map<Bitmap, Point> grassblock = new HashMap<Bitmap, Point>();
Map<Bitmap, Point> dirtblock = new HashMap<Bitmap, Point>();
Map<Bitmap, Point> grassSideBlock = new HashMap<Bitmap, Point>();
Map<Bitmap, Point> grassTopBlock = new HashMap<Bitmap, Point>();
Map<Bitmap, Point> orangeBlock = new HashMap<Bitmap, Point>();
Map<Bitmap, Point> levelMap = new HashMap<Bitmap, Point>();

List<MenuButton>menuButtonList=new ArrayList<MenuButton>(); 

public CreatorView(Context c) {
    super(c);
    this.c=c;


    WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    this.screenw= display.getWidth();
    this.screenh=display.getHeight();

    this.PixelDetect   = BitmapFactory.decodeResource(   getResources(),   R.drawable.custom_pixel);
    this.smallpixel   = Bitmap.createScaledBitmap(PixelDetect, 3, 3, false);

    this.grass=BitmapFactory.decodeResource(getResources(), R.drawable.block_grass);
    this.grassSide=BitmapFactory.decodeResource(getResources(), R.drawable.block_grassside);
    this.grassTop=BitmapFactory.decodeResource(getResources(), R.drawable.block_grasstop);
    this.orange=BitmapFactory.decodeResource(getResources(), R.drawable.block_cube1);
    this.dirt=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt);
    this.dirt2=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt2);
    this.dirt3=BitmapFactory.decodeResource(getResources(), R.drawable.block_dirt3);
    this.arrowno=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_noclick);
    this.arrown=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_normal);
    this.arrowl=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_left);
    this.arrowr=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_right);
    this.arrowu=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_up);
    this.arrowd=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_down);
    this.arrowul=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_upperleft);
    this.arrowur=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_upperright);
    this.arrowdl=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_downleft);
    this.arrowdr=BitmapFactory.decodeResource(getResources(), R.drawable.arrow_downright);
    this.arrowno=Bitmap.createScaledBitmap(arrowno, arrowno.getWidth()*3, arrowno.getHeight()*3, false);
    this.bin_Empty=BitmapFactory.decodeResource(getResources(), R.drawable.bin_empty);
    this.bin_Full=BitmapFactory.decodeResource(getResources(), R.drawable.bin_full);
    this.bin_Empty=Bitmap.createScaledBitmap(bin_Empty, bin_Empty.getWidth()*3, bin_Empty.getHeight()*3, false);
    this.bin_Full=Bitmap.createScaledBitmap(bin_Full, bin_Full.getWidth()*3, bin_Full.getHeight()*3, false);
    this.arrown=Bitmap.createScaledBitmap(arrown, arrown.getWidth()*3, arrown.getHeight()*3, false);
    this.arrowl=Bitmap.createScaledBitmap(arrowl, arrowl.getWidth()*3, arrowl.getHeight()*3, false);
    this.arrowr=Bitmap.createScaledBitmap(arrowr, arrowr.getWidth()*3, arrowr.getHeight()*3, false);
    this.arrowu=Bitmap.createScaledBitmap(arrowu, arrowu.getWidth()*3, arrowu.getHeight()*3, false);
    this.arrowd=Bitmap.createScaledBitmap(arrowd, arrowd.getWidth()*3, arrowd.getHeight()*3, false);
    this.arrowul=Bitmap.createScaledBitmap(arrowul, arrowul.getWidth()*3, arrowul.getHeight()*3, false);
    this.arrowur=Bitmap.createScaledBitmap(arrowur, arrowur.getWidth()*3, arrowur.getHeight()*3, false);
    this.arrowdl=Bitmap.createScaledBitmap(arrowdl, arrowdl.getWidth()*3, arrowdl.getHeight()*3, false);
    this.arrowdr=Bitmap.createScaledBitmap(arrowdr, arrowdr.getWidth()*3, arrowdr.getHeight()*3, false);

    Menu_Add(arrowno,0,true,"arrows");
    Menu_Add(bin_Empty,1,false,"bin");
    Menu_Add(grassTop,1,true,"grasstop");
    Menu_Add(grassSide,2,true,"grassside");
    Menu_Add(grass,3,true,"grass");
    Menu_Add(dirt,4,true,"dirt");
    Menu_Add(orange,5,true,"orange");

    arrowsp=new Point();
    arrowsp.x=0;
    arrowsp.y=0;
}
private void Menu_Add(Bitmap b,int order,boolean vertical,String name)
{
    Point p=new Point();
    p.x=0;
    p.y=0;
    if(order==0){
        p.x=0;
        p.y=0;
        MenuButton m=new MenuButton(order,b , vertical, p,name);
        menuButtonList.add(m);
    }
    else{
        for (MenuButton m : menuButtonList) {
            if((m.isVertical()==vertical||order==1)&&m.getOrder()+1==order ){
                if(vertical){
                    p.x=0;
                    p.y=m.getP().y+m.getBit().getHeight()+2;
                }
                else{
                    p.x=m.getP().x+m.getBit().getWidth()+2;
                    p.y=0;
                }
                MenuButton m2=new MenuButton(order,b , vertical, p,name);
                menuButtonList.add(m2);
            }
        }
    }
}

编辑 2:

我尝试使用 try 和 catch 来捕获错误并用 toast 显示它,但它没有显示就崩溃了。

try {
menuButtonList.add(m2);
} 
catch (Exception  e){
Toast.makeText(c, ""+e.getMessage(),Toast.LENGTH_LONG).show(); 
}

编辑 3: 我在列表上运行,它显示它有我添加到它的第一件事(箭头),如代码所示:

if(order==0){
        p.x=0;
        p.y=0;
        MenuButton m=new MenuButton(order,b , vertical, p,name);
        menuButtonList.add(m);
    }

,这意味着我可以向它添加东西,因为在 if(order==0) 上它添加它很好,但为什么在它之后,它崩溃了?

正如一些人在评论中提到的那样,如果没有堆栈跟踪,那么似乎没有任何理由相信异常是由您向我们展示的第三行代码引起的。在我看来,您可能在这里造成了 ConcurrentModificationException

for (MenuButton m : menuButtonList) {
    if((m.isVertical()==vertical||order==1)&&m.getOrder()+1==order ){
        // ...
        MenuButton m2=new MenuButton(order,b , vertical, p,name);
        menuButtonList.add(m2);
        // ...
    }
}

这是由于您在遍历列表时试图将项目添加到列表中造成的。相反,您最好创建一个单独的列表,然后在完成迭代后将其内容复制到 menuButtonList

List<MenuButton> newButtons = new ArrayList<MenuButton>();
for (MenuButton m : menuButtonList) {
    // ...
    MenuButton m2=new MenuButton(order,b , vertical, p,name);
    newButtons.add(m2);
    // ...
}
for (MenuButton newButton : newButtons) {
    menuButtonList.add(newButton);
}

请注意,在该循环中添加 menuButtonList.add(m2) 触发堆栈跟踪以报告该特定行上的错误,但在缺少其余代码的情况下, 完全有效。