Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid App is FC when "Back" or " Home" is pressed
    primarykey
    data
    text
    <p>I created an android app which plays a sound file. When I press the home button I noticed that the sound continues to play and does not stop so I added the following function inside my Class:</p> <pre><code>@Override protected void onStop() { super.onStop(); finish(); //kill the thread pS.reset(); //stop the sound } @Override protected void onPause() { super.onPause(); finish(); //kill the thread pS.reset(); //stop the sound } </code></pre> <p>Which does the job when "Back" or "Home" is pressed, it stops the music and the app does not force close. The only issue is, the sound has to be playing for it to not FC. If there is no sound playing, the app FC. I am guessing it's the </p> <blockquote> <p>pS.reset();</p> </blockquote> <p>function not knowing what to do when the sound isn't playing.</p> <p>If i set it to: </p> <blockquote> <p>pS = null;</p> </blockquote> <p>rather than pS.reset(), will it do the same without FC when "Back" or "Home" is pressed?</p> <p>My pS initialization:</p> <pre><code>pS = MediaPlayer.create(iAct.this, R.drawable.sound); </code></pre> <p>EDIT: I commented out</p> <pre><code>pS.reset(); </code></pre> <p>and added</p> <pre><code>pS = null; </code></pre> <p>it does not FC but the sound does not stop playing.</p> <p>I updated my onPause():</p> <pre><code>@Override protected void onPause() { super.onPause(); if (pS.isPlaying()) { pS.pause(); //stop the sound finish(); } else finish(); } </code></pre> <p>My Log:</p> <pre><code>04-02 13:06:40.563: E/AndroidRuntime(925): FATAL EXCEPTION: main 04-02 13:06:40.563: E/AndroidRuntime(925): java.lang.RuntimeException: Unable to pause activity {com.testcom.myapp/com.testcom.myapp.MyappActivity}: java.lang.NullPointerException 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.access$1700(ActivityThread.java:117) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.os.Handler.dispatchMessage(Handler.java:99) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.os.Looper.loop(Looper.java:123) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-02 13:06:40.563: E/AndroidRuntime(925): at java.lang.reflect.Method.invokeNative(Native Method) 04-02 13:06:40.563: E/AndroidRuntime(925): at java.lang.reflect.Method.invoke(Method.java:507) 04-02 13:06:40.563: E/AndroidRuntime(925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-02 13:06:40.563: E/AndroidRuntime(925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-02 13:06:40.563: E/AndroidRuntime(925): at dalvik.system.NativeStart.main(Native Method) 04-02 13:06:40.563: E/AndroidRuntime(925): Caused by: java.lang.NullPointerException 04-02 13:06:40.563: E/AndroidRuntime(925): at com.testcom.myapp.MyappActivity.onPause(MyappActivity.java:119) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.Activity.performPause(Activity.java:3851) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191) 04-02 13:06:40.563: E/AndroidRuntime(925): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341) 04-02 13:06:40.563: E/AndroidRuntime(925): ... 12 more </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload