-1

Here's the report:

java.lang.RuntimeException: Unable to start activity ComponentInfo{rs.androidaplikacije.zastaveigradovi/rs.androidaplikacije.zastaveigradovi.PogresanOdgovor}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
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:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at rs.androidaplikacije.zastaveigradovi.PogresanOdgovor.onCreate(PogresanOdgovor.java:56)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

It's basically a popup activity that says "Wrong answer" with some sound. Here's that class:

public class PogresanOdgovor extends Activity{
    MediaPlayer zvuk;
    TextView pogresno;
    String tacanOdgovor;
    String tacno;

    public final int delayTime = 2500;
    private Handler myHandler = new Handler();

    public void onUserInteraction(){
        myHandler.removeCallbacks(zatvoriPopup);
        myHandler.postDelayed(zatvoriPopup, delayTime);
    }
    private Runnable zatvoriPopup = new Runnable(){
        public void run(){
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

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

        setContentView(R.layout.pogresno);

        Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {
           tacno = extras.getString("tacanOdgovor");
        }

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

        myHandler.postDelayed(zatvoriPopup, delayTime);

        inicijalizujVarijable();

        if(zvuci == true){
            zvuk.start();
            }

            }

    private void inicijalizujVarijable() {
        Typeface pogresanFont = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        zvuk = MediaPlayer.create(this, R.raw.aah);

        pogresno = (TextView) findViewById(R.id.tvPogresno);

        pogresno.setTypeface(pogresanFont);
        pogresno.setText("Pogrešan odgovor!\nTačan odgovor je:\n\n" + tacno);

    }
    }

Error's on line:

zvuk.start();

It's where I call my sound.I don't get it. If sometimes works, why not always??

marjanbaz
  • 1,052
  • 3
  • 18
  • 35

1 Answers1

0

Have you released your MediaPlayer afterwards?

Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
  • You can see all of my code above. I did not know I should release it. When exactly should I release it? – marjanbaz May 13 '13 at 17:02
  • Use setOnCompletionListener() to install a listener which informs you when media was played, there you can release it. Additionally release it if you prematurely stop() the playing and don't want to continue. – Michael Butscher May 13 '13 at 17:17
  • Tried it and still get error reports. I don't get it...how come I get this error only once in a while, and not all the time?? If there is an error in my code, it should crash all the time. – marjanbaz May 14 '13 at 18:05