0

I have this problem again. In this thread I had the same problem with button click sound. Nothing helped, so I disabled that sound. No other solution. And now, I have the same crash report but now in another activity with check box sound. What the hell??? I never had this crash until I disabled the other sound. So I guess if I disable this sound I will have this problem with some other sound. So that's not the solution. Here the crash report:

java.lang.NullPointerException
at rs.androidaplikacije.zastaveigradovi.Izbor.onCheckedChanged(Izbor.java:126)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
at android.widget.RadioGroup.access$600(RadioGroup.java:52)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
at android.widget.CompoundButton.toggle(CompoundButton.java:86)
at android.widget.RadioButton.toggle(RadioButton.java:72)
at android.widget.CompoundButton.performClick(CompoundButton.java:98)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3821)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)

And activity code:

    public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(music == true){
            buttonClicks.start();
            }
            }

Error is on this line:

buttonClicks.start();

Here's the whole class:

public class Izbor extends Activity implements OnClickListener, OnCheckedChangeListener{

    public boolean music;
    MediaPlayer buttonClicks;
    MediaPlayer buttonBack;
    RadioButton rbDesetEasy,rbDvadesetEasy,rbDesetMedium,rbDvadesetMedium,rbDesetHard, rbDvadesetHard;
    Button dNazad, dStart;
    RadioGroup rGrupa;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        music = getPrefs.getBoolean("checkbox", true);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.izbor);
        addListenerOnButton();

    }

    private void addListenerOnButton() {

        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        Typeface pitanjeVrh = Typeface.createFromAsset(getAssets(), "Lobster.ttf");


        rbDesetEasy = (RadioButton) findViewById(R.id.radio1);
        rbDvadesetEasy = (RadioButton) findViewById(R.id.radio2);
        rbDesetMedium = (RadioButton) findViewById(R.id.radio3);
        rbDvadesetMedium = (RadioButton) findViewById(R.id.radio4);
        rbDesetHard = (RadioButton) findViewById(R.id.radio5);
        rbDvadesetHard = (RadioButton) findViewById(R.id.radio6);
        dNazad = (Button) findViewById(R.id.bNazad);
        dStart = (Button) findViewById(R.id.bStart);
        rGrupa = (RadioGroup) findViewById(R.id.radioGroup1);
        buttonBack = MediaPlayer.create(Izbor.this, R.raw.button31);
        tv1 = (TextView) findViewById(R.id.tv1);
        buttonClicks = MediaPlayer.create(this, R.raw.click);

        dNazad.setTypeface(dugmad);
        dStart.setTypeface(dugmad);
        rbDesetEasy.setTypeface(dugmad);
        rbDvadesetEasy.setTypeface(dugmad);
        rbDesetMedium.setTypeface(dugmad);
        rbDvadesetMedium.setTypeface(dugmad);
        rbDesetHard.setTypeface(dugmad);
        rbDvadesetHard.setTypeface(dugmad);
        tv1.setTypeface(pitanjeVrh);


        rGrupa.setOnCheckedChangeListener(this);
        rbDesetEasy.setOnClickListener(this);
        rbDvadesetEasy.setOnClickListener(this);
        rbDesetMedium.setOnClickListener(this);
        rbDvadesetMedium.setOnClickListener(this);
        rbDesetHard.setOnClickListener(this);
        rbDvadesetHard.setOnClickListener(this);
        dStart.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                if(rbDesetEasy.isChecked()){
                    Intent intent = new Intent(Izbor.this, Kviz.class);
                    intent.putExtra("myMethod", "nextQuestion");
                    startActivity(intent);
                }else if(rbDvadesetEasy.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDesetMedium.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz10Medium.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDvadesetMedium.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20Medium.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDesetHard.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz10Hard.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                    }else if(rbDvadesetHard.isChecked()){
                        Intent intent = new Intent(Izbor.this, Kviz20Hard.class);
                        intent.putExtra("myMethod", "nextQuestion");
                        startActivity(intent);
                }   
            }
        });


        dNazad.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(music == true){
                buttonBack.start();
                }
                finish();   
            }
        });
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(music == true){
        buttonClicks.start();
        }
        }

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

    }
}
Community
  • 1
  • 1
marjanbaz
  • 1,052
  • 3
  • 18
  • 35

1 Answers1

1

Your MediaPlayer appears to be null even though you try to instantiate it here

R.raw.click

According to the Docs

Returns a MediaPlayer object, or null if creation failed

so this tells us that your R.raw.clicks is probably missing, spelled incorrectly, or otherwise unavailable. This is causing the MediaPlayer creation to fail, hence leaving your buttonClicks as null

thegrinner
  • 11,546
  • 5
  • 41
  • 64
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • It's not missing, it's spelled correctly. It works on my phone, and on my girlfriend's phone. I can hear it. And I have around 10 000 downloads for this app, and only few of these crash reports, so it works fine in the rest of the cases. – marjanbaz May 17 '13 at 20:38
  • @marjanbaz Are you sure it's not related to the specific phone's model? – thegrinner May 17 '13 at 20:44
  • Well, I don't know that. Google does not share that info with crash report. Maybe. – marjanbaz May 17 '13 at 20:45
  • 1
    It must have something to do with accessing the `MediaPlayer` object itself the `raw resources` file on specific devices. Either way, it appears to be due to the `MediaPlayer` object not getting created. I would put a check in there before you call any method on it and if its `null` then tell the user their media player isn't compatible or something – codeMagic May 17 '13 at 20:54