我试图执行下面的方法但是我不断收到错误“跳过n帧!应用程序可能在其主线程上做了太多工作。”我已经尝试将for和while循环分解为这样的线程
Thread thread = new Thread() {
@Override
public void run() {
try {
// for or while loop here
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
然而,这根本不起作用。对此有任何可能的解决方法吗?
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--;
}
}
答案 0 :(得分:1)
你在for循环中运行的是什么? newSim()?该方法具有UI操作。所以它不能在不同的线程中运行。
答案 1 :(得分:0)
如果点已经存在于列表
中,为什么要添加该点while (intersection.contains(point)) {
intersection.add(point);
point = location.remove(random.nextInt(constraint));
}
您应该使用while(!intersaction.contains())