专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Android

使用自定义类时在view中抛出空指针错误

发布时间:2010-05-30 10:48:20 文章来源:www.iduyao.cn 采编人员:星星草
使用自定义类时在view中抛出空指针异常
我想在屏幕上倒计时三秒,然后随机显示一堆数字,大小和位置都是随机的。代码如下:
[code=Java][/code]
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");
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: