user1762507
user1762507

Reputation: 782

Why does my app work on my tablet but not on a phone?

Why does this app I made crash when I run it on my phone but it works perfectly fine on my tablet? When I run it on my phone get a java.NullPointerException at class.java:117 here is my entire activity.

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;
}

}

Upvotes: 0

Views: 419

Answers (1)

Stephen C
Stephen C

Reputation: 718698

The value of the home2 variable is null when home2.setOnClickListener(... is executed.

We cannot tell you why that is ... or how to fix it ... without more context.


You say:

home2 = (Button) findViewById(R.id.add2home); is run before it gets to this line

There are five possible explanations:

  • The findViewById(R.id.add2home) call is returning null.

  • Something is changing home2 after the findViewById call.

  • You are mistaken, and that statement is NOT executed BEFORE the use of home2.

  • You have misinterpreted the original stacktrace and the NullPointerException is not thrown by that statement.

  • The code that you are actually executing is out of sync with the source code that you showed us ... so that that exception is actually coming from somewhere else entirely.


UPDATE - based on the complete code you have now posted, it seems like my first explanation is the correct one. And @spanjeta's comment is very pertinent:

Can you please post the layout xml file. Do you have multiple layouts in your code. I think button id is incorrect in xml layout and that's why home2 is null.

Upvotes: 1

Related Questions