我希望你能幫助我。我不明白爲什麼這發生在我身上。Android onStop onDestroy onCreate後調用
我的應用程序在調用onCreate()後立即調用onPause(),onStop(),onDestroy()。屏幕是黑色的。當我按主頁按鈕時,onPause(),onStop(),onRestart(),onStart()和onResume()被調用,一切都很好。 這隻發生在我的真實設備上(xperia z2,kitkat 4.4.2)。 這從來不會發生在我的平板電腦(galaxy tab3,4.x)和許多其他模擬器上。
你們有解決這個問題的方法嗎?非常感謝你。
這裏的logcat的:
05-07 16:16:32.882: I/System.out(5149): onstart() called
05-07 16:16:32.882: I/System.out(5149): onresume() called
05-07 16:16:32.902: I/System.out(5149): onpause() called
05-07 16:16:32.902: I/System.out(5149): onstop() called
05-07 16:16:32.902: I/System.out(5149): ondestroy() called
05-07 16:16:33.002: D/dalvikvm(5149): GC_FOR_ALLOC freed 14423K, 28% free 59061K/80924K, paused 13ms, total 13ms
05-07 16:16:33.012: I/dalvikvm-heap(5149): Grow heap (frag case) to 72.570MB for 14745616-byte allocation
05-07 16:16:33.032: D/dalvikvm(5149): GC_FOR_ALLOC freed 9K, 10% free 73452K/80924K, paused 13ms, total 13ms
05-07 16:16:33.052: I/dalvikvm-heap(5149): Grow heap (frag case) to 79.953MB for 7750096-byte allocation
05-07 16:16:34.012: I/System.out(5149): onstart() called
05-07 16:16:34.012: I/System.out(5149): onresume() called
05-07 16:16:34.022: I/Choreographer(5149): Skipped 67 frames! The application may be doing too much work on its main thread.
05-07 16:16:34.072: I/Adreno-EGL(5149): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
05-07 16:16:34.072: I/Adreno-EGL(5149): OpenGL ES Shader Compiler Version: E031.24.00.06
05-07 16:16:34.072: I/Adreno-EGL(5149): Build Date: 02/12/14 Wed
05-07 16:16:34.072: I/Adreno-EGL(5149): Local Branch: au090
05-07 16:16:34.072: I/Adreno-EGL(5149): Remote Branch:
05-07 16:16:34.072: I/Adreno-EGL(5149): Local Patches:
05-07 16:16:34.072: I/Adreno-EGL(5149): Reconstruct Branch:
05-07 16:16:34.162: D/OpenGLRenderer(5149): Enabling debug mode 0
05-07 16:16:34.242: I/ActivityManager(5149): Timeline: Activity_idle id: [email protected] time:107086183
05-07 16:16:34.242: I/ActivityManager(5149): Timeline: Activity_idle id: [email protected] time:107086183
05-07 16:17:02.702: I/System.out(5149): onpause() called
05-07 16:17:03.232: W/IInputConnectionWrapper(5149): showStatusIcon on inactive InputConnection
05-07 16:17:03.292: I/System.out(5149): onstop() called
05-07 16:17:04.452: I/System.out(5149): onrestart() called
05-07 16:17:04.452: I/System.out(5149): onstart() called
05-07 16:17:04.452: I/System.out(5149): onresume() called
編輯: 這是所有的logcat拋出。 我剛剛發現,當我在橫向持有設備時,它不會調用onDestroy()。
我實現:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
和android:configChanges = 「方向| keyboardHidden」
...但仍然得到了同樣的問題。
代碼:
package com.example.x;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.google.android.gms.ads.*;
public class GameActivity extends Activity {
private static InterstitialAd interstitial;
private AdView adView;
private GameView theGameView;
private static SoundPool soundPool;
private static HashMap<Integer, Integer> soundsMap;
static int SOUND1=1;
static int SOUND2=2;
static int SOUND3=3;
static int SOUND4=4;
static int SOUND5=5;
static int SOUND6=6;
static int SOUND7=7;
static int SOUND8=8;
static int SOUND9=9;
static int SOUND10=10;
static int SOUND11=11;
static int SOUND12=12;
static int SOUND13=13;
static int SOUND14=14;
static int SOUND15=15;
static int SOUND16=16;
static int SOUND17=17;
static int SOUND18=18;
static int SOUND19=19;
static int SOUND20=20;
static int SOUND21=21;
static int SOUND22=22;
static int SOUND23=23;
static int SOUND24=24;
static int SOUND25=25;
static int SOUND26=26;
static int SOUND27=27;
static int SOUND28=28;
static float soundVol;
@SuppressLint("UseSparseArrays")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
theGameView = new GameView(this);
RelativeLayout layout = new RelativeLayout(this);
adView = new AdView(this);
adView.setAdUnitId("x");
adView.setAdSize(AdSize.SMART_BANNER);
adSetXY(0, GameView.getHeight-GameView.FieldHeight-GameView.FieldHeight/3);
layout.addView(theGameView);
layout.addView(adView);
setContentView(layout);
soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 100);
soundsMap = new HashMap<Integer, Integer>();
soundsMap.put(SOUND1, soundPool.load(this, R.raw.click, 1));
soundsMap.put(SOUND2, soundPool.load(this, R.raw.mpupgradetower, 1));
soundsMap.put(SOUND3, soundPool.load(this, R.raw.stower1, 1));
soundsMap.put(SOUND4, soundPool.load(this, R.raw.stower2, 1));
soundsMap.put(SOUND5, soundPool.load(this, R.raw.stower3, 1));
soundsMap.put(SOUND6, soundPool.load(this, R.raw.stower4, 1));
soundsMap.put(SOUND7, soundPool.load(this, R.raw.stower5, 1));
soundsMap.put(SOUND8, soundPool.load(this, R.raw.stower6, 1));
soundsMap.put(SOUND9, soundPool.load(this, R.raw.stower7, 1));
soundsMap.put(SOUND10, soundPool.load(this, R.raw.stower8, 1));
soundsMap.put(SOUND11, soundPool.load(this, R.raw.stower9, 1));
soundsMap.put(SOUND12, soundPool.load(this, R.raw.sspecial1, 1));
soundsMap.put(SOUND13, soundPool.load(this, R.raw.sspecial2, 1));
soundsMap.put(SOUND14, soundPool.load(this, R.raw.senemy1, 1));
soundsMap.put(SOUND15, soundPool.load(this, R.raw.senemy2, 1));
soundsMap.put(SOUND16, soundPool.load(this, R.raw.senemy3, 1));
soundsMap.put(SOUND17, soundPool.load(this, R.raw.senemy4, 1));
soundsMap.put(SOUND18, soundPool.load(this, R.raw.senemy5, 1));
soundsMap.put(SOUND19, soundPool.load(this, R.raw.senemy6, 1));
soundsMap.put(SOUND20, soundPool.load(this, R.raw.senemy7, 1));
soundsMap.put(SOUND21, soundPool.load(this, R.raw.senemy8, 1));
soundsMap.put(SOUND22, soundPool.load(this, R.raw.senemy9, 1));
soundsMap.put(SOUND23, soundPool.load(this, R.raw.sspecial, 1));
soundsMap.put(SOUND24, soundPool.load(this, R.raw.gameover, 1));
soundsMap.put(SOUND25, soundPool.load(this, R.raw.swater, 1));
soundsMap.put(SOUND26, soundPool.load(this, R.raw.stree, 1));
soundsMap.put(SOUND27, soundPool.load(this, R.raw.sfastattack, 1));
soundsMap.put(SOUND28, soundPool.load(this, R.raw.sleben, 1));
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("x");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
.addTestDevice("24610EF96304719510A4C412F8731837")
.addTestDevice("CAF0B70544C63E4A52F697BBF35A5BCA")
.build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
// Initiate a generic request.
AdRequest adRequest2 = new AdRequest.Builder()
.addTestDevice("B3EEABB8EE11C2BE770B684D95219ECB")
.addTestDevice("24610EF96304719510A4C412F8731837")
.addTestDevice("CAF0B70544C63E4A52F697BBF35A5BCA")
.build();
// Load the adView with the ad request.
adView.loadAd(adRequest2);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
public void adLoad()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
if(adView != null) {
adView.setEnabled(true);
adView.setVisibility(View.VISIBLE);
adView.resume();
}
}
});
}
public void adDestroy()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
if(adView != null) {
adView.setEnabled(false);
adView.setVisibility(View.GONE);
adView.pause();
}
}
});
}
public void adSetXY(float x, float y)
{
adView.setX(x);
adView.setY(y);
}
@Override
protected void onStart() {
super.onStart();
System.out.println("onstart() called");
// GameView.pause=true;
}
@Override
protected void onPause() {
super.onPause();
System.out.println("onpause() called");
GameView.pause=false;
GameLoopThread.paused=true;
}
@Override
protected void onStop() {
super.onStop();
System.out.println("onstop() called");
}
@Override
protected void onDestroy() {
adView.destroy();
System.out.println("ondestroy() called");
super.onDestroy();
// GameView.pause=true;
}
@Override
protected void onResume() {
super.onResume();
System.out.println("onresume() called");
GameLoopThread.unpaused=true;
}
@Override
protected void onRestart() {
System.out.println("onrestart() called");
super.onRestart();
// GameView.pause=true;
}
public static void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
發佈您的代碼。 –
發佈完整的logcat和代碼 –
我們仍然需要查看您的活動代碼以提供幫助。 – indivisible