2
votes

Im trying to perform the method below however I keep getting the error "Skipped n frames! The application may be doing too much work on its main thread." Ive tried breaking the for and while loops into threads like this

 Thread thread = new Thread() {
@Override
public void run() {
    try {
        // for or while loop here
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
};

thread.start();

However that didnt work at all either. Any possible fixes for this?

public void newSim(int x) {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    int buttonWidth = (int) (width * 0.15);
    int buttonWidth1 = (int) (width * 0.20);
    int buttonHeight1 = (int) (height * 0.20);

    ArrayList<Point> intersection = new ArrayList<Point>();
    final ArrayList<Point> location = new ArrayList<Point>();

    int constraint = 0;

    int xC = width - buttonWidth1;
    int yC = height - buttonWidth1;

    for (int i = 0; i < x; i++) {
        for (int j = 0; j < x; i++) {
            location.add(new Point(i, j));
            constraint++;
        }
    }

    for (int i = 0; i < x; i++) {
        Random random = new Random();
        Point point = new Point();
        point = location.remove(random.nextInt(constraint));
        while (intersection.contains(point)) {
            intersection.add(point);
            point = location.remove(random.nextInt(constraint));
        }
    Button but1 = new Button(this);
    but1.setText(String.valueOf(i));
    RelativeLayout r = (RelativeLayout) findViewById(R.id.playLayout);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(buttonWidth, buttonWidth);
    layoutParams.setMargins(0, 0, buttonWidth, buttonWidth);
    but1.setLayoutParams(layoutParams);
    r.addView(but1);
    constraint--;
    }
}
2
Is this on an emulator or a real device?Ted Hopp
On an emulator, could that be the reason why? EDIT: I actually tried it on a phone just now same issueMJJLAM
I've seen this on the ADB logs a lot, especially when using the emulator, and I don't think its a really bad issueJames Parsons
Ye just tried it on a phone, still doesnt work.MJJLAM

2 Answers

1
votes

What are you running in the for loop? newSim()? That method has UI operations on it. So it can't be running in a different thread.

0
votes

Why you are adding the point if its already exists in the list

while (intersection.contains(point)) {
            intersection.add(point);
            point = location.remove(random.nextInt(constraint));
}

You should use while(!intersaction.contains())