我想在屏幕上倒计时三秒,然后随机显示一堆数字,大小和位置都是随机的。代码如下:package com.passion;import java.util.ArrayList;
import java.util.Random;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.TextView;public class GameActivity extends Activity {  
    private MyCount mc = null;  
    private TextView tv = null; 
    private NewView view = null;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub   
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.game); 
        Intent intent = getIntent();
        view = new NewView(this);
        //设置背景
       Resources res = getResources();
       Drawable drawable = res.getDrawable(R.drawable.bac);
       this.getWindow().setBackgroundDrawable(drawable);
        //设置字体
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/LithosPro-Bold.ttf");
        tv = (TextView)findViewById(R.id.show);
        tv.setTypeface(typeface);
        tv.setTextColor(Color.BLACK);
        //倒计时5s,每次间隔1s
        mc = new MyCount(5000, 1000);  
        mc.start();  
        
    }
     
  
/*定义一个倒计时的内部类*/  
    class MyCount extends CountDownTimer { 
    
        public MyCount(long millisInFuture, long countDownInterval) {     
            super(millisInFuture, countDownInterval);     
        }  
        
        //倒计时结束之后做的事
        @Override     
        public void onFinish() {  
            tv.setVisibility(View.INVISIBLE);
            setContentView(view);            
        }
        
        //倒计时的时候
        @Override     
        public void onTick(long millisUntilFinished) {     
            tv.setText("请等待(" + millisUntilFinished / 1000 + ")...");
               
        }    
    } 
        
} /*定义一个显示数字的类*/
 class NewView extends AbsoluteLayout implements OnClickListener{
private int count = 5;
private int range = 10;
private TextView[] text = new TextView[count];
private int[] numbers = null;
private DisplayMetrics dm;
private int width;
private int height;
private Location[] location = null;

public NewView(Context context) {
super(context);
// TODO Auto-generated constructor stub
//获取屏幕的尺寸,以像素为单位
dm = getResources().getDisplayMetrics();
     width = dm.widthPixels;
     height = dm.heightPixels;
//设置字体
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/LithosPro-Bold.ttf");

//设置随机数
numbers = randRange(count,range);

//设置坐标
location = new Location[count];
Log.i("mes","haha");
location = randLocation(count,width-50,height-50);
Log.i("mes","heihei");

for(int i =0;i<count;i++){
text[i] = new TextView(context);
addView(text[i],new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT,AbsoluteLayout.LayoutParams.FILL_PARENT, location[i].x, location[i].y));
text[i].setText(numbers[i]+"");
text[i].setTextSize(50);
text[i].setTypeface(typeface);
text[i].setTextColor(Color.BLACK);
text[i].setId(numbers[i]);

}

}


//自定义一个坐标类
class Location{
private int x;
private int y;
}

//获得随机数组,num是产生随机数的个数,max是取值范围
public int[] randRange(int num ,int max) {
      ArrayList<Integer> list = new ArrayList<Integer>();
      Random rand = new Random();
      while(true){
            int rm = rand.nextInt(max);
            if(!list.contains(rm)){
            list.add(rm);
            if(list.size()>=num)break;
       }
   }
     int result[] = new int[num];
     for(int i = 0;i<list.size();i++){
     result[i] = list.get(i);
   }
   return result;
 }

//获得随机坐标,num是产生随机数的个数,rangex,rangey是x,y轴取值范围
public Location[] randLocation(int num,int rangex,int rangey){
Location[] location = new Location[num];
for(int k = 0;k<num;k++){
location[k].x = -1;
location[k].y = -1;
}
Random rand = new Random();
B:for(int i = 0;i<num;i++){
int numx = rand.nextInt(rangex);
int numy = rand.nextInt(rangey);
for(int j = 0;j<i;j++){
//如果不符合条件再循环
if(isIntersect(location[i].x,location[i].y,location[j].x,location[j].y) == false)
i--;
continue B;
}

location[i].x = numx;
location[i].y = numy; }
return location;
}

//判断两个矩形有没有相交:两个左上角的横坐标距离大于50,且纵坐标距离大于50,
//否则相交,这里设置为60是为了使数字之间有间隙美观一些
    public boolean isIntersect(int x1,int y1,int x2,int y2){
     if(Math.abs(x1-x2)>60&&Math.abs(y1-y2)>60)
     return true;
     else 
     return false;        
    } @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
    

}我自定义了一个坐标类Location来存放坐标数组,并且用isIntersect函数来判断是否相交,randLocation函数负责返回符合条件的不相交的坐标数组,结果运行时候抛出了NullPointerException01-29 13:24:27.569: I/mes(484): haha
01-29 13:24:27.569: D/AndroidRuntime(484): Shutting down VM
01-29 13:24:27.579: W/dalvikvm(484): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-29 13:24:27.609: E/AndroidRuntime(484): FATAL EXCEPTION: main
01-29 13:24:27.609: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.passion/com.passion.GameActivity}: java.lang.NullPointerException
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.os.Looper.loop(Looper.java:123)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-29 13:24:27.609: E/AndroidRuntime(484):  at java.lang.reflect.Method.invokeNative(Native Method)
01-29 13:24:27.609: E/AndroidRuntime(484):  at java.lang.reflect.Method.invoke(Method.java:507)
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-29 13:24:27.609: E/AndroidRuntime(484):  at dalvik.system.NativeStart.main(Native Method)
01-29 13:24:27.609: E/AndroidRuntime(484): Caused by: java.lang.NullPointerException
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.passion.NewView$Location.access$2(GameActivity.java:117)
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.passion.NewView.randLocation(GameActivity.java:143)
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.passion.NewView.<init>(GameActivity.java:99)
01-29 13:24:27.609: E/AndroidRuntime(484):  at com.passion.GameActivity.onCreate(GameActivity.java:32)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-29 13:24:27.609: E/AndroidRuntime(484):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-29 13:24:27.609: E/AndroidRuntime(484):  ... 11 more用Log测了问题出现在location = randLocation(count,width-50,height-50);这一句。我想不通哪里有问题,请大神帮我指出该怎么改,谢谢!!!