Android 从横幅广告回来后应用程序崩溃
当我点击横幅广告时,它的打开状态良好,但当我回到应用程序时,它迫使应用程序崩溃并关闭。这是一个声板应用程序 我应该向应用程序添加什么,以便在关闭或返回应用程序后恢复活动时不会崩溃 只是一个初学者,所以任何帮助都将不胜感激 活动1Android 从横幅广告回来后应用程序崩溃,android,android-layout,admob,ads,Android,Android Layout,Admob,Ads,当我点击横幅广告时,它的打开状态良好,但当我回到应用程序时,它迫使应用程序崩溃并关闭。这是一个声板应用程序 我应该向应用程序添加什么,以便在关闭或返回应用程序后恢复活动时不会崩溃 只是一个初学者,所以任何帮助都将不胜感激 活动1 public class Activity1 extends Activity { int selectedSoundId; MediaPlayer player; @Override public void onCreate(Bund
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();
}
}
公共类活动1扩展活动{
int-selectedSoundId;
媒体播放器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
AdView mAdView=(AdView)findviewbyd(R.id.adMob);
AdRequest AdRequest=新建AdRequest.Builder().build();
mAdView.loadAd(adRequest);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
player=新媒体播放器();
最终资源res=getResources();
final int[]buttonId={};
final int[]soundid={};
View.OnClickListener=newview.OnClickListener(){
公共void onClick(视图v){
对于(int i=0;i
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>
崩溃日志:
致命异常:主
进程:com.example.keshav.giantsoundboard,PID:5366
java.lang.RuntimeException:无法暂停活动
{com.example.keshav.giantsoundboard/com.example.keshav.giantsoundboard.Activity3}:
java.lang.IllegalStateException
在
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381)
在
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
在
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)
位于android.app.ActivityThread.-wrap13(ActivityThread.java)
在
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:148)
位于android.app.ActivityThread.main(ActivityThread.java:5417)
位于java.lang.reflect.Method.invoke(本机方法)
在
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
原因:java.lang.IllegalStateException
在android.media.MediaPlayer.\u停止(本机方法)
在android.media.MediaPlayer.stop(MediaPlayer.java:1231)
在
com.example.keshav.giantsoundboard.Activity3.onPause(Activity3.java:95)
位于android.app.Activity.performPause(Activity.java:6348)
在
android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1311)
在
android.app.ActivityT
protected void onPause() {
if (player != null) {
//player.stop();
player.release();
}
super.onPause();
}