0

为什么我在手机上运行这个应用程序时崩溃了,但在我的平板电脑上却运行良好?当我在手机上运行它时,在 class.java:117 处得到一个java.NullPointerException 是我的全部活动。

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Basketball extends Activity {

    int homecount, awaycount, minutes, seconds, sec, min;
        TextView home, away, timeEx;
    Button stop, start, reset, addhome, subhome, addaway, subaway, home2,
        home3, away2, away3;

    jakes.apps.scoreboard.CountDownTimer time;
    public boolean running;
    public int msElapsed;
    TextView time2;
    long startTime, stopTime;
    Button button;
    long timeWhenStopped; 
    int buttonCount, period;
    int ending;
    MediaPlayer ourSong;
    SharedPreferences getPrefs;
    getSystemService(Context.VIBRATOR_SERVICE);
    RadioButton first, second, third, fourth;

private void change() {
    // TODO Auto-generated method stub
    if ((buttonCount % 2) == 0) {
        start.setText("Start");
        stop.setText("Stop");
    } else {
        start.setText("Resume");
        stop.setText("Reset");
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basketball);

    initize();

    time = new jakes.apps.scoreboard.CountDownTimer(900000, 1000) {

        public void onTick(long millisUntilFinished) {
            sec--;
            if (sec == 0) {
                min--;
                sec = 59;
            }
            if (sec < 10) {
                time2.setText(min + ":0" + sec);
            }
            time2.setText(min + ":" + sec);
        }

        public void onFinish() {
            if (period == 1) {
                Toast.makeText(getApplicationContext(),
                        "First period over", Toast.LENGTH_LONG).show();
                fourth.setChecked(false);
                third.setChecked(false);
                second.setChecked(true);
                first.setChecked(false);
                period++;

            } else if (period == 2) {
                Toast.makeText(getApplicationContext(),
                        "First period over", Toast.LENGTH_LONG).show();
                fourth.setChecked(false);
                third.setChecked(true);
                second.setChecked(false);
                first.setChecked(false);
                period++;

            } else if (period == 3) {
                Toast.makeText(getApplicationContext(),
                        "First period over", Toast.LENGTH_LONG).show();
                fourth.setChecked(true);
                third.setChecked(false);
                second.setChecked(false);
                first.setChecked(false);
                period++;

            } else if (period == 4) {
                gameover();

        }
    };

    addhome.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            homecount++;
            home.setText(homecount + "");
            boolean music = getPrefs.getBoolean("mute", false);
            if (music == false) {
                ourSong.start();
            }

        }
    });
    home2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            homecount = homecount + 2;
            home.setText(homecount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
            }

        }

    });
    home3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            homecount = homecount + 3;
            home.setText(homecount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
            }

        }
    });

    subhome.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            homecount--;
            home.setText(homecount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
            }

        }
    });
    addaway.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            awaycount++;
            away.setText(awaycount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        }
    });
    away2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            awaycount = awaycount + 2;
            away.setText(awaycount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        }
    });
    away3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            awaycount = awaycount + 3;
            away.setText(awaycount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        }
    });

    subaway.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            awaycount--;
            away.setText(awaycount + "");
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        }
    });

}

protected void gameover() {
    time2.setText("15:00");
    period = 0;
    fourth.setChecked(false);
    third.setChecked(false);
    second.setChecked(false);
    first.setChecked(true);
    if (homecount < awaycount) {
        Toast.makeText(getApplicationContext(), "Away team wins!!!",
                Toast.LENGTH_LONG).show();
    } else if (homecount > awaycount) {
        Toast.makeText(getApplicationContext(), "Home team wins!!!",
                Toast.LENGTH_LONG).show();
    } else if (homecount == awaycount) {
        Toast.makeText(getApplicationContext(), "Its a tie",
                Toast.LENGTH_LONG).show();
    }

}

private void initize() {
    // TODO Auto-generated method stub
    time2 = (TextView) findViewById(R.id.timer);
    start = (Button) findViewById(R.id.bStart);
    stop = (Button) findViewById(R.id.bStop);
    home3 = (Button) findViewById(R.id.add3home);
    home2 = (Button) findViewById(R.id.add2home);
    away3 = (Button) findViewById(R.id.add3away);
    away2 = (Button) findViewById(R.id.add2away);
    addhome = (Button) findViewById(R.id.bAddHome);
    addaway = (Button) findViewById(R.id.bAddAway);
    subhome = (Button) findViewById(R.id.bSubHome);
    subaway = (Button) findViewById(R.id.bSubAway);
    home = (TextView) findViewById(R.id.home);
    away = (TextView) findViewById(R.id.away);
    running = false;
    stopTime = 0;
    stopTime = 0;
    timeWhenStopped = 0;
    buttonCount = 1;

    button = (Button) findViewById(R.id.bStart);
    button.setOnClickListener(mStartListener);

    button = (Button) findViewById(R.id.bStop);
    button.setOnClickListener(mStopListener);

    ourSong = MediaPlayer.create(Basketball.this, R.raw.click);
    getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    min = 14;
    sec = 60;
    period = 1;

    first = (RadioButton) findViewById(R.id.first);
    second = (RadioButton) findViewById(R.id.second);
    third = (RadioButton) findViewById(R.id.third);
    fourth = (RadioButton) findViewById(R.id.fourth);
}

View.OnClickListener mStartListener = new OnClickListener() {
    public void onClick(View v) {
        start();
    }

};

View.OnClickListener mStopListener = new OnClickListener() {
    public void onClick(View v) {
        stop();

    }

};

private void stop() {
    // TODO Auto-generated method stub
    running = false;
    buttonCount++;
    change();
    time.pause();

    if ((buttonCount % 2) == 0) {
        reset();
    } else {

    }
    boolean music = getPrefs.getBoolean("mute", true);
    if (music == false) {
        ourSong.start();
        ourSong.release();
    }
}

protected void start() {
    if (running == true) {

    } else {
        if (((buttonCount % 2) == 0)) {
            time.resume();
            running = true;
            buttonCount++;
            change();
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        } else {

            buttonCount++;
            time.start();
            running = true;
            change();
            boolean music = getPrefs.getBoolean("mute", true);
            if (music == false) {
                ourSong.start();
                ourSong.release();
            }
        }
    }

}

private void reset() {
    // TODO Auto-generated method stub
    time.cancel();
    boolean music = getPrefs.getBoolean("mute", true);
    if (music == false) {
        ourSong.start();
        ourSong.release();
    }
    buttonCount++;
    sec = 1;
    time2.setText("15:00");
    min = 15;
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater blowUp = getMenuInflater();
    blowUp.inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.newgame:
        Intent NewGame = new Intent("jakes.apps.scoreboard.NEWGAME");
        startActivity(NewGame);
        break;
    case R.id.aboutus:
        Intent AboutUs = new Intent("jakes.apps.scoreboard.ABOUTUS");
        startActivity(AboutUs);
    case R.id.settings:

        Intent settings = new Intent("jakes.apps.scoreboard.SETTINGS");
        startActivity(settings);
        break;

    }
    return true;
}

}

4

1 回答 1

1

home2变量的值是执行的null时间home2.setOnClickListener(...

如果没有更多上下文,我们无法告诉您为什么会这样……或如何解决它……


你说:

home2 = (Button) findViewById(R.id.add2home); 在到达这条线之前运行

有五种可能的解释:

  • 电话正在findViewById(R.id.add2home)返回。null

  • 通话home2 后发生了一些变化。findViewById

  • 你错了,在使用home2.

  • 您误解了原始堆栈跟踪,并且NullPointerException该语句没有抛出。

  • 您实际执行的代码与您向我们展示的源代码不同步……因此该异常实际上完全来自其他地方。


更新- 根据您现在发布的完整代码,我的第一个解释似乎是正确的。@spanjeta 的评论非常中肯:

能否请您发布布局xml文件。您的代码中是否有多个布局。我认为 xml 布局中的按钮 ID 不正确,这home2就是null.

于 2012-11-18T03:35:48.033 回答