1. Are you ready for the Galaxy S20? Here is everything we know so far!

App stalls depending on sharedpreferences selected

Discussion in 'Android Development' started by saintratchet, Mar 21, 2016.

  1. saintratchet

    saintratchet Lurker
    Thread Starter

    Hi,

    I'm developing a version of the Mastermind game as an Android app and I've hit a bit of a wall. I have the game coded out and I have an options menu set up using Shared Preferences but when certain options are selected and I click on play, the app stops working and cuts to a black screen accepting no new input.
    The game works perfectly fine when I have selected the option for a 4 Peg code with 4, 5 or 6 colours but when I select a 6 Peg code it only works when I have selected 6 colours. When I select a 6 Peg code with 4 or 5 colours the app stalls as mentioned above.
    Any help with this at all would be greatly appreciated, I've been stuck on this for a few days and can't wrap my head around what could be causing the issue.

    Here is my onCreate method that takes in the preferences and loads up the necessary button images.
    Code (Text):
    1.  
    2. MediaPlayer mp;
    3. int codeLength;
    4. int[] code = {0};
    5. ImageButton orangeButton, purpleButton;
    6. private static final String TAG = MainActivity.class.getSimpleName();
    7.  
    8. @Override
    9. protected void onCreate(Bundle savedInstanceState) {
    10.     super.onCreate(savedInstanceState);
    11.  
    12.     SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());   // SHARED PREFERENCES
    13.     boolean boo = SP.getBoolean("duplicates", false);
    14.     String str = SP.getString("pegs", "1");
    15.     String colour = SP.getString("colours", "4");
    16.     Log.w(TAG, "Peg number is: " + str + "and Colour number is: " + colour);
    17.  
    18.     if(str.equalsIgnoreCase("1")){
    19.         setContentView(R.layout.activity_main);
    20.     }
    21.     else if(str.equalsIgnoreCase("2"))
    22.         setContentView(R.layout.game_6);
    23.     else
    24.         setContentView(R.layout.activity_main_test);
    25.  
    26.     ImageButton redButton = (ImageButton) findViewById(R.id.redPeg);
    27.     redButton.setOnClickListener(this);
    28.  
    29.     ImageButton greenButton = (ImageButton) findViewById(R.id.greenPeg);
    30.     greenButton.setOnClickListener(this);
    31.  
    32.     ImageButton blueButton = (ImageButton) findViewById(R.id.bluePeg);
    33.     blueButton.setOnClickListener(this);
    34.  
    35.     ImageButton yellowButton = (ImageButton) findViewById(R.id.yellowPeg);
    36.     yellowButton.setOnClickListener(this);
    37.  
    38.     ImageButton codeButton = (ImageButton) findViewById(R.id.CodeButton);
    39.     codeButton.setOnClickListener(this);
    40.  
    41.     Button lineButton = (Button) findViewById(R.id.LineButton);
    42.     lineButton.setOnClickListener(this);
    43.  
    44.     purpleButton = (ImageButton) findViewById(R.id.purplePeg);
    45.     purpleButton.setOnClickListener(this);
    46.  
    47.     orangeButton = (ImageButton) findViewById(R.id.orangePeg);
    48.     orangeButton.setOnClickListener(this);
    49.  
    50.     setColourPegs(Integer.parseInt(colour));
    51.  
    52.     Peg.pegFalse();
    53.  
    54.     Code.setDuplicates(boo);
    55.  
    56.  
    57.     if(str.equalsIgnoreCase("2"))
    58.         codeLength = 6;
    59.     else
    60.         codeLength = 4;
    61.  
    62.  
    63.     Code.setColours(Integer.parseInt(colour));
    64.  
    65.     Code.setCodeLength(codeLength);
    66.     Check.setCodeLength(codeLength);
    67.  
    68.     code = Code.Generate();
    69. }
    70.  
    And here is the setColours method

    Code (Text):
    1.  
    2. public void setColourPegs(int colour){
    3.  
    4.     if(colour == 5){
    5.         purpleButton.setBackgroundColor(Color.parseColor("#ffffff"));
    6.         purpleButton.setImageResource(R.drawable.purple_peg2);
    7.     }
    8.     else if (colour == 6){
    9.         purpleButton.setBackgroundColor(Color.parseColor("#ffffff"));
    10.         purpleButton.setImageResource(R.drawable.purple_peg2);
    11.  
    12.         orangeButton.setBackgroundColor(Color.parseColor("#ffffff"));
    13.         orangeButton.setImageResource(R.drawable.orange_peg2);
    14.     }
    15. }
    16.  
    Like I said, any help at all would be appreciated and if anyone needs to see any other classes or methods please just ask and I'd be more than happy to help.
    Thanks.
     


  2. Deleted User

    Deleted User Guest

    When you say the app "stops working", does it crash?
    Do you get any errors in the Logcat window?

    Have you tried setting a breakpoint in method setColourPegs() and step through the code to see what happens?
     
  3. saintratchet

    saintratchet Lurker
    Thread Starter

    Hi, it's not necessarily a crash. Just more the app moves to a new screen but the screen is blank and then the app freezes, I guess would be the best way to describe it. I can't do anything when it freezes other than press the home button.

    I don't get any errors in the log window and just from the fact that it works for 4 Pegs but not 6 Pegs is really whats confusing me.

    Haven't tried a breakpoint yet (still pretty new to Android), but I will try and give it a go. I set up a log message to ensure that I was getting the correct values for colour and str from preferences and they were both correct when I ran it.
     
  4. saintratchet

    saintratchet Lurker
    Thread Starter

    I was just playing with the app there and I got a new error message in the logcat that I hadn't got previously. I selected 6 Pegs and 4 colours and instead of pressing play straight away, I read the how to play section of the app.
    So about 10 seconds after clicking the options, I pressed play and the screen loaded up properly :)

    The new logcat message displayed was: "Skipped 95 frames! The application may be doing too much work on its main thread."

    So I'm guessing this is the problem, though I don't know why that would explain the app having difficulties in loading 6 Pegs 4 colours and not 6 Pegs 6 colours.
     
  5. Deleted User

    Deleted User Guest

    It would help if you showed the code you are using to handle the play button click.

    I think that logcat message is a red herring. The Android runtime gets a bit paranoid about clogging up the main UI thread. My apps get that message all the time, but it doesn't seem to affect their operation.
     
  6. saintratchet

    saintratchet Lurker
    Thread Starter

    Alright, heres onClick:
    Code (Text):
    1.  
    2. public void onClick(View v) {
    3.  
    4.     int[] code = codeGenerator();
    5.  
    6.  
    7.     Toast pieceToast;
    8.     ImageButton imgBus;
    9.     String buttonID;
    10.     int resID;
    11.     int z = 0;
    12.     int[] pins;
    13.  
    14.     boolean b;
    15.     boolean win;
    16.  
    17.     switch (v.getId()) {
    18.  
    19.         case R.id.redPeg:
    20.  
    21.             b = Full();
    22.             if (b == false) {
    23.                 z = pegPosition();
    24.                 buttonID = "pegimage" + String.valueOf(z);
    25.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    26.                 imgBus = ((ImageButton) findViewById(resID));
    27.                 imgBus.setImageResource(R.drawable.red_peg_nobg);
    28.                 imgBus.setTag("1");
    29.                 Check.setPegCode(1);
    30.             } else {
    31.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    32.                 pieceToast.show();
    33.             }
    34.             break;
    35.  
    36.         case R.id.greenPeg:
    37.  
    38.             b = Full();
    39.             if (b == false) {
    40.                 z = pegPosition();
    41.                 buttonID = "pegimage" + String.valueOf(z);
    42.                 // buttonID = position();
    43.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    44.                 imgBus = ((ImageButton) findViewById(resID));
    45.                 imgBus.setImageResource(R.drawable.green_peg2_nobg);
    46.                 imgBus.setTag("3");
    47.                 Check.setPegCode(3);
    48.             } else {
    49.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    50.                 pieceToast.show();
    51.             }
    52.  
    53.             break;
    54.  
    55.         case R.id.bluePeg:
    56.  
    57.             b = Full();
    58.             if (b == false) {
    59.                 z = pegPosition();
    60.                 buttonID = "pegimage" + String.valueOf(z);
    61.                 // buttonID = position();
    62.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    63.                 imgBus = ((ImageButton) findViewById(resID));
    64.                 imgBus.setImageResource(R.drawable.blue_peg2_nobg);
    65.                 imgBus.setTag("2");
    66.                 Check.setPegCode(2);
    67.             } else {
    68.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    69.                 pieceToast.show();
    70.             }
    71.  
    72.             break;
    73.  
    74.         case R.id.yellowPeg:
    75.             b = Full();
    76.             if (b == false) {
    77.                 z = pegPosition();
    78.                 buttonID = "pegimage" + String.valueOf(z);
    79.                 // buttonID = position();
    80.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    81.                 imgBus = ((ImageButton) findViewById(resID));
    82.                 imgBus.setImageResource(R.drawable.yellow_peg2_nobg);
    83.                 imgBus.setTag("4");
    84.                 Check.setPegCode(4);
    85.             } else {
    86.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    87.                 pieceToast.show();
    88.             }
    89.  
    90.             break;
    91.  
    92.         case R.id.purplePeg:
    93.             b = Full();
    94.             if (b == false) {
    95.                 z = pegPosition();
    96.                 buttonID = "pegimage" + String.valueOf(z);
    97.                 // buttonID = position();
    98.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    99.                 imgBus = ((ImageButton) findViewById(resID));
    100.                 imgBus.setImageResource(R.drawable.purple_peg2_nobg);
    101.                 imgBus.setTag("5");
    102.                 Check.setPegCode(5);
    103.             } else {
    104.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    105.                 pieceToast.show();
    106.             }
    107.  
    108.             break;
    109.  
    110.         case R.id.orangePeg:
    111.             b = Full();
    112.             if (b == false) {
    113.                 z = pegPosition();
    114.                 buttonID = "pegimage" + String.valueOf(z);
    115.                 // buttonID = position();
    116.                 resID = getResources().getIdentifier(buttonID, "id", getPackageName());
    117.                 imgBus = ((ImageButton) findViewById(resID));
    118.                 imgBus.setImageResource(R.drawable.orange_peg2_nobg);
    119.                 imgBus.setTag("6");
    120.                 Check.setPegCode(6);
    121.             } else {
    122.                 pieceToast = Toast.makeText(getApplicationContext(), "Press the Check Button", Toast.LENGTH_SHORT);
    123.                 pieceToast.show();
    124.             }
    125.  
    126.             break;
    127.  
    128.         case R.id.CodeButton:
    129.  
    130.             String displayCode = "";
    131.             for (int q = 0; q < code.length; q++) {
    132.                 displayCode += String.valueOf(code[q]);
    133.             }
    134.  
    135.             pieceToast = Toast.makeText(getApplicationContext(), displayCode, Toast.LENGTH_SHORT);
    136.             pieceToast.show();
    137.  
    138.  
    139.             break;
    140.  
    141.         case R.id.LineButton:
    142.  
    143.             b = Full();
    144.             if(b == false){
    145.                 pieceToast = Toast.makeText(getApplicationContext(), "Please fill in all available peg slots", Toast.LENGTH_SHORT);
    146.                 pieceToast.show();
    147.             }
    148.             else {
    149.                 Peg.pegRelease();
    150.                 win = Check.CheckGame(code);
    151.                 pins = Check.ReturnPins();
    152.                 drawPin(pins);
    153.                 //toastPins(pins);
    154.                 if (win == true) {
    155.  
    156.  
    157.                     final View bubble = v;
    158.                     mp = MediaPlayer.create(this, R.raw.win_noise);
    159.                     mp.start();
    160.                     String message = getString(R.string.dialog_text);
    161.                     AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    162.                     alertDialog.setTitle("Congratulations! You win");
    163.                     alertDialog.setMessage(message);
    164.                     alertDialog.setPositiveButton("Play", new DialogInterface.OnClickListener() {
    165.                         public void onClick(DialogInterface dialog, int which) {
    166.                             //Intent intent = new Intent(bubble.getContext(), MainActivity.class);
    167.                             //startActivityForResult(intent, 0);
    168.                             Intent mIntent = getIntent();
    169.                             finish();
    170.                             startActivity(mIntent);
    171.                             stopMedia();
    172.                         }
    173.                     });
    174.                     alertDialog.setNegativeButton("Menu", new DialogInterface.OnClickListener() {
    175.                         public void onClick(DialogInterface dialog, int which) {
    176.                             finish();
    177.                             stopMedia();
    178.                         }
    179.                     });
    180.                     alertDialog.show();
    181.                 }
    182.             }
    183.  
    184.             break;
    185.  
    186.         default:
    187.             break;
    188.     }
    189.  
    190. }
    191.  
     
  7. saintratchet

    saintratchet Lurker
    Thread Starter

    Figured out the issue!!! I hadn't set up anything to default a 6 Peg 4 or 5 colour system to automatically allow duplicates. Thanks for the help, really made me comb through the code again.
     
    Deleted User likes this.
Loading...
Similar Threads - App stalls depending
  1. samaspotion
    Replies:
    0
    Views:
    0
  2. Ron Landers
    Replies:
    1
    Views:
    117
  3. WittFreely
    Replies:
    0
    Views:
    117
  4. OM2
    Replies:
    3
    Views:
    176
  5. 010Alex010
    Replies:
    2
    Views:
    173
  6. spl4000
    Replies:
    5
    Views:
    283
  7. denerex
    Replies:
    0
    Views:
    180
  8. tealapps
    Replies:
    2
    Views:
    161
  9. App Update
    Replies:
    0
    Views:
    190
  10. mad_rover
    Replies:
    1
    Views:
    175

Share This Page

Loading...