当前位置: 代码迷 >> Android >> android利用countdowntimer切换屏幕内容时报错解决办法
  详细解决方案

android利用countdowntimer切换屏幕内容时报错解决办法

热度:106   发布时间:2016-05-01 22:12:42.0
android利用countdowntimer切换屏幕内容时报错
[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.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.widget.AbsoluteLayout;
import android.widget.TextView;

public class NewActivity extends Activity {  
  private MyCount mc;  
  private TextView tv; 
  private NewView view;
  @Override  
  protected void onCreate(Bundle savedInstanceState) {  
  // TODO Auto-generated method stub  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.main); 
  Log.i("line 29","1");
  view = new NewView(null);
  Log.i("line 31","2");
  //设置背景
  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("请等待5秒(" + millisUntilFinished / 1000 + ")...");
   
  }  
  } 
   
   
   
}

/*定义一个显示数字的类*/
class NewView extends AbsoluteLayout /*implements OnClickListener*/{
private int count = 5;
private TextView[] text = new TextView[count];
private int[] locationx = null;
private int[] locationy = null;
private int[] numbers = null;
private DisplayMetrics dm;
private int width;
private int height;


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,10);
locationx = randRange(count,width-40);
locationy = randRange(count,height-40);

for(int i =0;i<count;i++){
text[i] = new TextView(context);
addView(text[i],new AbsoluteLayout.LayoutParams(100,100, locationx[i], locationy[i]));
  相关解决方案