0

我是android应用程序的初学者。我已经创建了我的第一个应用程序来播放res目录中Raw文件夹中的声音我的代码做得很好并且运行良好然后我继续停止声音我会这样做但是当我旋转移动或 puss 移动返回按钮时错误应用程序停止....发生了。我该如何解决这个问题。

    @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
4

3 回答 3

2

在您的清单中,进行以下更改。

<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.
于 2013-06-27T10:43:24.667 回答
0

试试这个代码...

在 onCreate() 覆盖 onBackPressed() 之后将其放入类文件中。

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

并将以下属性与 Activity 寄存器放在AndroidManifest.xml文件中。

android:screenOrientation="portrait"

如果您需要 screen potrait/Landscape ,请根据您的要求。

于 2013-06-27T12:24:47.927 回答
0

变量在使用之前mp必须设置为适当的值(好的地方是onCreate()onResume())(就像它发生在 中一样onPause()

于 2013-06-27T18:31:12.423 回答