android手机摇一摇产生双色球七个号码程序意外停止?

默写那思念love 发布于 2014/03/16 14:12
阅读 328
收藏 1

  在编写这个应用时,apk文件安装到手机上,第一次可以运行,第二次就提示程序意外停止,而且不能使劲儿的摇,只要使劲儿,程序就不给面子,就停止了,在eclipse中也没有提示错误的信息,请问这是我手机的问题,还是代码出现的问题额?希望前辈们在闲暇之余可以帮忙看看,谢谢!

package com.jzgx.t21_sensortest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.jzgx.t21_sensortest.R.drawable;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 1.获得重力感应器的服务
 * 2.
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
 
 private SensorManager sensorManager;
 private float x,y,z;
 private ImageView[] iv=new ImageView[6];
 private ImageView[] iv1=new ImageView[1];
 private LinearLayout ll;
 
 public static int[] imgs={R.drawable.a01,R.drawable.a02,R.drawable.a03,R.drawable.a04,R.drawable.a05,R.drawable.a06,
  R.drawable.a07,R.drawable.a08,R.drawable.a09,R.drawable.a10,R.drawable.a11,R.drawable.a12,
  R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16,R.drawable.a17,R.drawable.a18,
  R.drawable.a19,R.drawable.a20,R.drawable.a21,R.drawable.a22,R.drawable.a23,R.drawable.a24,
  R.drawable.a25,R.drawable.a26,R.drawable.a27,R.drawable.a28,R.drawable.a29,R.drawable.a30,
  R.drawable.a31,R.drawable.a32,R.drawable.a33
  };
 public static int[] imag={R.drawable.b01,R.drawable.b02,R.drawable.b03,R.drawable.b04,R.drawable.b05,R.drawable.b06,
  R.drawable.b07,R.drawable.b08,R.drawable.b09,R.drawable.b10,R.drawable.b11,R.drawable.b12,
  R.drawable.b13,R.drawable.b14,R.drawable.b15,R.drawable.b16};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //获得服务
  sensorManager=(SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
  //获得感应器,要求指定感应器的类型Sensor.TYPE_ACCELEROMETER,加速度感应器
  Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

  sensorManager.registerListener(eventListener, sensor, SensorManager.SENSOR_DELAY_GAME); 
  //控件
  ll=(LinearLayout) this.findViewById(R.id.ll);
  for(int i=0;i<iv.length;i++){
   iv[i]=new ImageView(this);
   ll.addView(iv[i]);
  }
        for(int j=0;j<iv1.length;j++){
      iv1[j]=new ImageView(this);
      ll.addView(iv1[j]);
     }
 }
    
 private SensorEventListener eventListener=new SensorEventListener() {
  
  @Override
  public void onSensorChanged(SensorEvent event) {
   //得到坐标值
   x=event.values[SensorManager.DATA_X];
   y=event.values[SensorManager.DATA_Y];
   z=event.values[SensorManager.DATA_Z];
   //设置晃动的阀值
   int num=19;
   if(Math.abs(x)>num||Math.abs(y)>num||Math.abs(z)>num){
    //产生随机数
    List<Integer> numList1=getRandomNum();
    List<Integer> numList2=getRandomNum1();
    Animation anmi=AnimationUtils.loadAnimation(MainActivity.this, R.anim.myanim);
    Animation anmi1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.myanim);
    for(int i=0;i<iv.length;i++){
     int n=numList1.get(i);
     iv[i].setImageResource(imgs[n]);
     
     iv[i].setAnimation(anmi);
    }
    for(int j=0;j<iv1.length;j++){
     int nu=numList2.get(j);
     iv1[j].setImageResource(imag[nu]);
     iv1[j].setAnimation(anmi1);
     
    }
   }
  }
  
  private List<Integer> getRandomNum1() {
   // TODO Auto-generated method stub
   Random rd2=new Random();
   List<Integer> list=new ArrayList<Integer>();
   while(true){
    int nu=(int) (rd2.nextDouble()*16+1);
    if(!list.contains(nu)){
     list.add(nu);
    }
    if(list.size()>1){
     break;
    }
   }
   return list;
  }

  @Override
  public void onAccuracyChanged(Sensor arg0, int arg1) {
   
  }
 };
 
 public List<Integer> getRandomNum(){
  Random rd1=new Random();
  
  //用来存放1~32之间7个随机数的集合
  List<Integer> list=new ArrayList<Integer>();
  //生成这些号码
  while(true){
   //反复生成随机号
   int n=(int)(rd1.nextDouble()*33)+1;
   
   //先判断集合中有木有,如果木有就加进去,如果有就不加
   if(!list.contains(n)){
    list.add(n);
    
   }
   //如果集合加满到7个就生成完毕
   if(list.size()>6){
    break;
   }
  }
  //排序
  Collections.sort(list);
  return list;
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

加载中
返回顶部
顶部