从横幅广告返回后应用程序崩溃
App crashing after coming back from banner ads
当我点击横幅广告时,它可以正常打开,但当我返回应用程序时,它会强制应用程序崩溃并关闭。这是一个音板应用程序。
我应该向应用程序添加什么,以使其在关闭或返回应用程序后 activity 恢复时不会崩溃。
只是初学者,如有任何帮助,我们将不胜感激。
活动 1
public class Activity1 extends Activity {
int selectedSoundId;
MediaPlayer player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
AdView mAdView = (AdView) findViewById(R.id.adMob);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { };
final int[] soundIds = { };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res
.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
protected void onPause() {
super.onPause();
player.stop();
player.release();
player.pause();
}
}
Activity1.xml
<RelativeLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adMob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
</RelativeLayout>
崩溃日志:
FATAL EXCEPTION: main
Process: com.example.keshav.giantsoundboard, PID: 5366
java.lang.RuntimeException: Unable to pause activity
{com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity3}:
java.lang.IllegalStateException
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
at
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)
at android.app.ActivityThread.-wrap13(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._stop(Native Method)
at android.media.MediaPlayer.stop(MediaPlayer.java:1231)
at
com.example.keshav.giantsoundboard.Activity3.onPause(Activity3.java:95)
at android.app.Activity.performPause(Activity.java:6348)
at
android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3367)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
at
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)
at android.app.ActivityThread.-wrap13(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
After release(), the MediaPlayer object is no longer available.
在 activity 的 onPause()
方法中,您在释放 player
.[=16= 后调用 MediaPlayer
对象上的 pause()
方法]
所以删除 player.release();
问题显然是在媒体播放器上有 IllegalStateException 的崩溃日志。
java.lang.IllegalStateException at
android.media.MediaPlayer._stop(Native Method) at
android.media.MediaPlayer.stop(MediaPlayer.java:1231
重写你的 onPause()
你不能在释放媒体检查时暂停播放器 MediaState diagram below.
protected void onPause() {
if (player != null) {
//player.stop();
player.release();
}
super.onPause();
}
当我点击横幅广告时,它可以正常打开,但当我返回应用程序时,它会强制应用程序崩溃并关闭。这是一个音板应用程序。
我应该向应用程序添加什么,以使其在关闭或返回应用程序后 activity 恢复时不会崩溃。
只是初学者,如有任何帮助,我们将不胜感激。
活动 1
public class Activity1 extends Activity {
int selectedSoundId;
MediaPlayer player;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
AdView mAdView = (AdView) findViewById(R.id.adMob);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player = new MediaPlayer();
final Resources res = getResources();
final int[] buttonIds = { };
final int[] soundIds = { };
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < buttonIds.length; i++) {
if (v.getId() == buttonIds[i]) {
selectedSoundId = soundIds[i];
AssetFileDescriptor afd = res
.openRawResourceFd(soundIds[i]);
player.reset();
try {
player.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
break;
}
}
}
};
for (int i = 0; i < buttonIds.length; i++) {
Button soundButton = (Button) findViewById(buttonIds[i]);
registerForContextMenu(soundButton);
soundButton.setOnClickListener(listener);
}
}
protected void onPause() {
super.onPause();
player.stop();
player.release();
player.pause();
}
}
Activity1.xml
<RelativeLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adMob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
</RelativeLayout>
崩溃日志:
FATAL EXCEPTION: main Process: com.example.keshav.giantsoundboard, PID: 5366 java.lang.RuntimeException: Unable to pause activity {com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity3}: java.lang.IllegalStateException at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315) at android.app.ActivityThread.-wrap13(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IllegalStateException at android.media.MediaPlayer._stop(Native Method) at android.media.MediaPlayer.stop(MediaPlayer.java:1231) at com.example.keshav.giantsoundboard.Activity3.onPause(Activity3.java:95) at android.app.Activity.performPause(Activity.java:6348) at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3367) at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340) at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315) at android.app.ActivityThread.-wrap13(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
After release(), the MediaPlayer object is no longer available.
在 activity 的 onPause()
方法中,您在释放 player
.[=16= 后调用 MediaPlayer
对象上的 pause()
方法]
所以删除 player.release();
问题显然是在媒体播放器上有 IllegalStateException 的崩溃日志。
java.lang.IllegalStateException at android.media.MediaPlayer._stop(Native Method) at android.media.MediaPlayer.stop(MediaPlayer.java:1231
重写你的 onPause()
你不能在释放媒体检查时暂停播放器 MediaState diagram below.
protected void onPause() {
if (player != null) {
//player.stop();
player.release();
}
super.onPause();
}