为什么我的应用在我的平板电脑上工作但在手机上无法工作?

时间:2012-11-18 03:29:51

标签: android

为什么这个应用程序在我的手机上运行时发生了崩溃,但它在我的平板电脑上工作得很好?当我在手机上运行时,在 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;
}

}

1 个答案:

答案 0 :(得分:1)

执行home2时,null变量的值为home2.setOnClickListener(...

我们无法告诉你为什么会这样......或者如何解决它...没有更多的背景。


你说:

  

home2 =(按钮)findViewById(R.id.add2home);在到达此行之前运行

有五种可能的解释:

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

  • home2电话后,findViewById 正在发生变化。

  • 您错了,并且在使用home2之前未执行该声明。

  • 您误解了原始堆栈跟踪,并且该语句未引发NullPointerException

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


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

  

请你发布布局xml文件。您的代码中是否有多个布局?我认为xml布局中的按钮ID不正确,这就是home2null的原因。