Egydeveloper
Egydeveloper

Reputation: 195

error application stopped when rotate mobile

I am beginner at android app. I had created my first app to play sound from Raw folder at res directory i did my code well and it run good then i go on stopping the sound i did it will but when i rotate the mobile or puss mobile back button the error application stopped .... happened . how can i solve this issue .

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{ 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) 
{ 
   mp= MediaPlayer.create(Ramadan.this,R.raw.sound); 

if(mp.isPlaying()) 
{
    mp.stop();
    return true; 
}

} 
else    return false;
    return false;

}

@Override
protected void onStart() {
    super.onStart(); 
}

@Override 
protected void onResume() {

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);

    registerReceiver(listener, intentFilter);

    super.onResume();
}
@Override
protected void onStop() {
    super.onStop();

    if(mp.isPlaying())
    {
      mp.stop();
      mp.release();
    }
}


@Override
protected void onPause() {

    super.onPause();

    if(mp.isPlaying())
    {
      mp.stop();
      mp.release();
    }
}

<activity
        android:name="com.x.x"
        android:label="@string/app_name"
        android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" > 


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.ACTION_SCREEN_OFF"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

06-27 17:57:59.422: E/Trace(636): error opening trace file: No such file or directory (2)
06-27 17:58:00.452: I/Choreographer(636): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-27 17:58:00.492: D/gralloc_goldfish(636): Emulator without GPU emulation detected.
06-27 17:58:00.992: I/Choreographer(636): Skipped 74 frames!  The application may be doing too much work on its main thread.
06-27 17:58:02.432: D/dalvikvm(636): GC_CONCURRENT freed 115K, 3% free 8346K/8519K, paused 6ms+27ms, total 112ms
06-27 17:58:02.432: D/dalvikvm(636): WAIT_FOR_CONCURRENT_GC blocked 67ms
06-27 17:58:08.902: D/AndroidRuntime(636): Shutting down VM
06-27 17:58:08.902: W/dalvikvm(636): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-27 17:58:08.982: E/AndroidRuntime(636): FATAL EXCEPTION: main
06-27 17:58:08.982: E/AndroidRuntime(636): java.lang.RuntimeException: Unable to pause activity {com.ramadan/com.ramadan.Ramadan}: java.lang.NullPointerException
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2838)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2794)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2772)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.access$800(ActivityThread.java:130)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.os.Looper.loop(Looper.java:137)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-27 17:58:08.982: E/AndroidRuntime(636):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 17:58:08.982: E/AndroidRuntime(636):  at java.lang.reflect.Method.invoke(Method.java:511)
06-27 17:58:08.982: E/AndroidRuntime(636):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-27 17:58:08.982: E/AndroidRuntime(636):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-27 17:58:08.982: E/AndroidRuntime(636):  at dalvik.system.NativeStart.main(Native Method)
06-27 17:58:08.982: E/AndroidRuntime(636): Caused by: java.lang.NullPointerException
06-27 17:58:08.982: E/AndroidRuntime(636):  at com.ramadan.Ramadan.onPause(Ramadan.java:152)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.Activity.performPause(Activity.java:5106)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1225)
06-27 17:58:08.982: E/AndroidRuntime(636):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2825)
06-27 17:58:08.982: E/AndroidRuntime(636):  ... 12 more
06-27 17:58:11.552: I/Process(636): Sending signal. PID: 636 SIG: 9

Upvotes: 0

Views: 270

Answers (3)

Vaishali Sharma
Vaishali Sharma

Reputation: 602

In your manifest, do the following changes.

<activity android:name=".EULAActivity"
android:theme="@android:style/Theme.Translucent"
android:configChanges="keyboard|keyboardHidden|orientation" />

android:configChanges is the attribute, you need to use in your application.

Upvotes: 1

Michael Butscher
Michael Butscher

Reputation: 10969

Variable mp must be set to an appropriate value (good places are onCreate() or onResume()) before it is used (as it happens in onPause())

Upvotes: 0

Mr.Sandy
Mr.Sandy

Reputation: 4349

Try this code...

Put it in class file after over onCreate() override onBackPressed().

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    finish();
}

and put following property with Activity register in AndroidManifest.xml file.

android:screenOrientation="portrait"

as per your requirements if you need screen potrait/Landscape.

Upvotes: 0

Related Questions