定时器NullPointerException

时间:2012-09-02 17:53:41

标签: android timer nullpointerexception

我收到了一个关于Timer活动的最后一行的NullPointerException。活动中发生的事情是5个.ogg文件一个接一个地播放。错误指向最后一个TimerTask。我发布了整个代码,请不要害怕,代码中有很多重复。我不知道错误之前用户做了什么,它不会出现在我的手机上。

java.lang.NullPointerException
at com.b2creativedesigns.eyetest.AmslerGrid1$5.run(AmslerGrid1.java:343)
at java.util.Timer$TimerImpl.run(Timer.java:284)

部分代码:

public class AmslerGrid1 extends Activity {

    Button btnFinish;
    Button btnMute;
    ImageView imgAG;
    TextView tvResult;
    MediaPlayer help1, help2, help3, help4, help5;
    int paused=0;
    long del=250;
    int intentcounter=0;
    int p=10;
    int length;
    private Timer timer;
    boolean out;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.amslergrid1);
        out = false;
        imgAG = (ImageView)findViewById(R.id.imgAG);
        tvResult = (TextView)findViewById(R.id.tvAGResult);
        btnFinish = (Button)findViewById(R.id.btnAGResult);
        btnMute = (Button)findViewById(R.id.btnAGmute);

        help1 = MediaPlayer.create(AmslerGrid1.this, R.raw.ag1);
        help2 = MediaPlayer.create(AmslerGrid1.this, R.raw.ag2);
        help3 = MediaPlayer.create(AmslerGrid1.this, R.raw.ag3);
        help4 = MediaPlayer.create(AmslerGrid1.this, R.raw.ag4);
        help5 = MediaPlayer.create(AmslerGrid1.this, R.raw.ag5);

        timer = new Timer("AmslerGridTimer");
        timer.schedule(TimerHelp1, 2000);

        help1.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(final MediaPlayer cim) {
                if (paused == 0)
                {
                }
                help1.release();help1=null;
                timer = new Timer("AmslerGridTimer");
                timer.schedule(TimerHelp2, 1500);
            }
        });

        help2.setOnCompletionListener(new OnCompletionListener() {...
        help3.setOnCompletionListener(new OnCompletionListener() {...
        help4.setOnCompletionListener(new OnCompletionListener() {...
        help5.setOnCompletionListener(new OnCompletionListener() {...

            @Override
            public void onCompletion(final MediaPlayer cim) {
                if (paused == 0)
                {
                }
                help5.release();help5=null;
            }
        });

    }






    @Override
    protected void onPause() {
        super.onPause();
        if (help1 != null)
        {
            help1.pause(); 
            length=help1.getCurrentPosition();
        }
        ...
        else if (help5 != null)
        {
            help5.pause(); 
            length=help5.getCurrentPosition();
        }

        p=0;
    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (help1 != null)
        {
            if (p==0){
                help1.seekTo(length); help1.start(); 
                help1.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(final MediaPlayer cim) {
                        help1.release();help1=null;
                    }
                });
                paused=0;p=1;
            }
        }
           ...
        else if (help5 != null)
        {
            if (p==0){
                help5.seekTo(length); help5.start(); 
                help5.setOnCompletionListener(new OnCompletionListener() {

                    @Override
                    public void onCompletion(final MediaPlayer cim) {
                        help5.release();help5=null;
                    }
                });
                paused=0;p=1;
            }
        }
    }

    @Override
      public void onDestroy() {
        super.onDestroy();
        timer.cancel();
        timer = null;
      }

    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
        {
            out = true;
            if (help1 != null)
            {
                help1.stop(); 
            }
            else if (help2 != null)
            {
                help2.stop(); 
            }
            else if (help3 != null)
            {
                help3.stop(); 
            }
            else if (help4 != null)
            {
                help4.stop(); 
            }
            else if (help5 != null)
            {
                help5.stop(); 
            }
            Intent intentstart = new Intent(AmslerGrid1.this, MainActivity.class);
            intentstart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intentstart);

        }
        return super.onKeyDown(keyCode, event);
    }

    private TimerTask TimerHelp1 = new TimerTask() {
        @Override
        public void run() {
            if (!out) help1.start();p=1;
        }
      };
    private TimerTask TimerHelp2 = new TimerTask() {
        @Override
        public void run() {
            if (!out) help2.start();p=1;
        }
      };
    private TimerTask TimerHelp3 = new TimerTask() {
        @Override
        public void run() {
            if (!out) help3.start();p=1;
        }
      };
    private TimerTask TimerHelp4 = new TimerTask() {
        @Override
        public void run() {
            if (!out) help4.start();p=1;
        }
      };
    private TimerTask TimerHelp5 = new TimerTask() {
        @Override
        public void run() {
            if (!out) help5.start();p=1; //error line. Maybe out is null?
        } 
      };
}

0 个答案:

没有答案