我是 android 应用的初学者。我创建了我的第一个应用程序来播放原始文件夹中 res 目录中的声音我做了我的代码很好并且它运行良好然后我继续停止声音我做了它但是当我旋转移动或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
答案 0 :(得分: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.
答案 1 :(得分:0)
试试这段代码......
在onCreate()覆盖onBackPressed()之后将它放在类文件中。
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
finish();
}
并在 AndroidManifest.xml 文件中将以下属性与活动注册放在一起。
android:screenOrientation="portrait"
根据您的要求,如果您需要屏幕 potrait / Landscape 。
答案 2 :(得分:0)
变量mp
必须在使用之前设置为适当的值(好位置为onCreate()
或onResume()
)(在onPause()
中发生)