挣扎于NullPointerException

时间:2011-06-15 17:11:14

标签: java android timer nullpointerexception countdown

总计新手程序员在这里试图制作他的第一个Android应用程序。一个简单的倒数计时器和一些其他的东西,它都工作,但为了尝试解决倒数计时器的问题我将该部分代码从开始按钮onClick内移动到活动的onCreate(或至少我想我有)。在我的脑海中,我认为这意味着倒数计时器已准备好运行,但在用户点击开始按钮之前不会实际运行。

无论如何,使用下面的代码,当活动加载时我得到NullPointerException并且我不确定在哪里,或者我做了什么是远程良好编程(我最近看了很多YouTube视频并得到了这里有很多其他问题的帮助。)

任何帮助都会很受欢迎:)

package com.asurya.hulktimer;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity {

    long starttime = 180000;
    long count = 1000;
    long timechange = 30000;
int missionpoints = 0;
boolean isPaused = false;
boolean clockrunning = false;

Context context = getApplicationContext();
CharSequence text = "Clock is running - create some pause code";
int duration = Toast.LENGTH_LONG;




/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.asurya.hulktimer.R.layout.main);


 // THIS SECTION ENABLES THE FEEDBACK FOR ALL THE BUTTONS

    final Vibrator vibe = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

 // THIS SECTION DEFINES THE FONT FOR ALL THE OBJECTS IN THE VIEW       

   final Button but_start = (Button) findViewById(com.asurya.hulktimer.R.id.but_start);
   Typeface font = Typeface.createFromAsset(getAssets(), "CASLANTR.TTF");
   but_start.setTypeface(font);

   final Button but_reset = (Button) findViewById(com.asurya.hulktimer.R.id.but_reset);
   but_reset.setTypeface(font);

   final TextView txt = (TextView) findViewById(com.asurya.hulktimer.R.id.timer);
   txt.setText(formatTime(starttime));
   txt.setTypeface(font);

   final TextView txt_mt_1 = (TextView)      findViewById(com.asurya.hulktimer.R.id.lbl_missiontally_1);
   txt_mt_1.setTypeface(font);

   final TextView txt_mt_2 = (TextView) findViewById(com.asurya.hulktimer.R.id.lbl_missiontally_2);
   txt_mt_2.setTypeface(font);

   final TextView txt_mt_value = (TextView) findViewById(com.asurya.hulktimer.R.id.lbl_mt_value);
   txt_mt_value.setTypeface(font);

   final TextView txt_cp_value = (TextView) findViewById(com.asurya.hulktimer.R.id.lbl_cp_value);
   txt_cp_value.setTypeface(font);

   final Button but_add_time = (Button) findViewById(com.asurya.hulktimer.R.id.but_add);
   but_add_time.setTypeface(font);

   final Button but_minus_time = (Button) findViewById(com.asurya.hulktimer.R.id.but_minus);
   but_minus_time.setTypeface(font);

   final Button but_generate_cp = (Button) findViewById(com.asurya.hulktimer.R.id.but_generate_cp);
   but_generate_cp.setTypeface(font);

   final Button but_mp_add = (Button) findViewById(R.id.but_mp_add);
   but_mp_add.setTypeface(font);

   final Button but_mp_minus = (Button) findViewById(com.asurya.hulktimer.R.id.but_mp_minus);
   but_mp_minus.setTypeface(font);

   final Button but_mp_reset = (Button) findViewById(com.asurya.hulktimer.R.id.but_mp_reset);
   but_mp_reset.setTypeface(font);

   {

       //  THIS SECTION IS THE COMMAND POINTS CODE

       final MediaPlayer mp = MediaPlayer.create(this, R.raw.generate);

       Button but_generate_cp1 = (Button) findViewById(com.asurya.hulktimer.R.id.but_generate_cp);
       but_generate_cp1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            vibe.vibrate(50);
            mp.start();

            final Random random = new Random();
            int mycps  = random.nextInt(6);
            final int myCommandPoints = mycps + 1;

            txt_cp_value.setText("" + String.valueOf(myCommandPoints));
        }
    });
   }

   // THIS SECTION IS THE COUNTDOWN TIMER

   final MediaPlayer oneminrem = MediaPlayer.create(this, R.raw.ping);

   final CountDownTimer gameclock = new CountDownTimer(starttime, count) {

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

        txt.setText("" + formatTime(millisUntilFinished));

        long oneMinuteRemaining = millisUntilFinished /1000;

        if (oneMinuteRemaining == 60){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 30){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 15){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 10){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 9){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 8){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 7){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 6){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 5){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 4){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 3){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 2){
            oneminrem.start();
        }
        if (oneMinuteRemaining == 1){
            oneminrem.start();
        }


    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        txt.setText("00:00");
        but_start.setText("Start");
        clockrunning = false;
    }
};

// THIS SECTION IS THE TIMER BUTTONS

    Button but_start1 = (Button) findViewById(R.id.but_start);
    but_start1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        vibe.vibrate(50);

        if (clockrunning = false) {
            gameclock.start();
        }

        if (clockrunning = true) {

            // Display a Toast message to confirm code working.
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

        }
    });




       Button but_reset1 = (Button) findViewById(R.id.but_reset);
   but_reset1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        if (clockrunning == true){

        }
    }
});



   //THIS SECTIONS IS THE BUTTONS WHICH ADD OR SUBTRACT TIME FROM THE STARTING TIME

   Button but_add1 = (Button) findViewById(R.id.but_add);
   but_add1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        if (clockrunning == false) {
        starttime = starttime + timechange;
        txt.setText(formatTime(starttime));
        }

    }
});

   Button but_minus1 = (Button) findViewById(R.id.but_minus);
   but_minus1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        if (clockrunning == false) {
        starttime = starttime - timechange;
        txt.setText(formatTime(starttime));
        }

    }
});


   //THIS SECTION HANDLES THE MISSIONS POINTS CODE

   txt_mt_value.setText("" + String.valueOf(missionpoints));

   Button but_missionpoint_add = (Button) findViewById(R.id.but_mp_add);
   but_missionpoint_add.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        missionpoints = missionpoints + 1;
        txt_mt_value.setText("" + String.valueOf(missionpoints));

    }
});

   Button but_missionpoint_minus = (Button) findViewById(R.id.but_mp_minus);
   but_missionpoint_minus.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        missionpoints = missionpoints - 1;
        txt_mt_value.setText("" + String.valueOf(missionpoints));
    }
});

   Button but_missionpoints_reset = (Button) findViewById(R.id.but_mp_reset);
   but_missionpoints_reset.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        vibe.vibrate(50);
        missionpoints = 0;
        txt_mt_value.setText("" + String.valueOf(missionpoints));
    }
});
}

    /* (non-Javadoc)
 * @see android.app.Activity#finish()
 */
@Override
public void finish() {
    // TODO Auto-generated method stub
    super.finish();
}

public String formatTime(long millis) {
 //   String output = "00:00";
    long seconds = millis / 1000;
    long minutes = seconds / 60;

    seconds = seconds % 60;
    minutes = minutes % 60;

    String secondsD = String.valueOf(seconds);
    String minutesD = String.valueOf(minutes);

    if (seconds < 10)
      secondsD = "0" + seconds;
    if (minutes < 10)
      minutesD = "0" + minutes;

    String output = minutesD + " : " + secondsD;
    return output;
  }  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.Info:
    Intent myInfoMenuIntent = new Intent(main.this, Info.class);
    startActivity(myInfoMenuIntent);
        return true;
    case R.id.Credits:
        Intent myCreditMenuIntent = new Intent(main.this, Credits.class);
        startActivity(myCreditMenuIntent);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
}

4 个答案:

答案 0 :(得分:1)

类型行中的空指针异常

A.B.C.D

意味着

  • A为空
  • 或A.B为空
  • 或A.B.C为空

点左边的东西是空的。

因此,提供中间变量并在控制台中打印它们以检测哪个变量为空。

此致  斯特凡

答案 1 :(得分:1)

考虑制作类的UI元素(例如txt_cp_value)成员变量,然后在onCreate方法中分配它们。这样他们就可以从匿名的OnClickListener访问它们。从我所看到的情况来看,txt_cp_value.setText("" + String.valueOf(myCommandPoints));可能是罪魁祸首。

你有堆栈跟踪吗?

答案 2 :(得分:0)

“一个简单的倒数计时器和其他一些东西,它都工作,但为了尝试解决倒数计时器的问题,我将该部分代码从开始按钮onClick中移动到活动的onCreate “

如果这是真的,那么您移动到onCreate方法的代码很可能会尝试使用onCreate方法中未初始化的内容,但它在onClick方法的开始按钮中初始化。在代码中查找类似的内容。

关于代码。这远非有效:

if (oneMinuteRemaining == 60){
    oneminrem.start();
}
if (oneMinuteRemaining == 30){
    oneminrem.start();
}

你应该在第一个'if'之后使用'else if'。

答案 3 :(得分:0)

修正了它

Context context = getApplicationContext();
CharSequence text = "Clock is running - create some pause code";
int duration = Toast.LENGTH_LONG;

是有罪的代码。将其重新定位在onClick侦听器按钮内。这只是测试内容的临时代码。

再次感谢所有人的帮助。我现在比昨天更了解Java和堆栈跟踪:)