android调试报错,求助!!!
想做一个程序,动态加载多张图片用textview同时显示,位置的坐标和显示的数字都随机,所以采用了代码布局和absolutelayout。我写的代码如下;
package com.passion;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.TextView;
public class GameActivity extends Activity {
private TextView[] text;
private AbsoluteLayout.LayoutParams lp[];
private int count;
private DisplayMetrics dm;
int width,height;
private int[] number,locationX,locationY;
private OnClickListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
AbsoluteLayout layout = new AbsoluteLayout(this);
setContentView(layout);
//设置背景
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.bac);
this.getWindow().setBackgroundDrawable(drawable);
//设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/LithosPro-Bold.ttf");
//获取屏幕的尺寸
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
count = 3;
text = new TextView[count];
number = randRange(count,10);
locationX = new int[count];
locationX = randRange(count,width-60);
locationY = new int[count];
locationY = randRange(count,height-60);
for(int i = 0;i<count;i++){
text[i] = new TextView(this);
text[i].setTypeface(typeface);
text[i].setText(number[i]);
text[i].setTextSize(60);
lp[i] = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,locationX[i],locationY[i]);
text[i].setLayoutParams(lp[i]);
layout.addView(text[i]);
}
}
//对屏幕坐标取随机值,num是取值的个数,range是取值范围
public int[] randRange(int num,int range){
Random rand = new Random();
boolean flag = true;
int result[] = new int[num];
result[0] = rand.nextInt(range);
int count = 1;
while(flag&&count<num){
flag = false;
result[count] = rand.nextInt(range);
if(result[count]==result[count-1])
break;
else{
count++;
flag=true;
}
}
return result;
}
}
用log.i试了下,错误好像在循环里,但是具体的我搞不清楚,求助各位大神,到底哪里需要改的?不吝赐教了各位,谢谢!!!!