سؤال

I hope you can help me. I don't understand why this is happening to me.

My app is calling onPause(), onStop(), onDestroy() immediately after calling onCreate(). The screen is black. When I press the home button, onPause(), onStop(), onRestart(), onStart() and onResume() are called and everything is fine. This only happens on my real device (xperia z2, kitkat 4.4.2). This does never happen on my tablet (galaxy tab3, 4.x) and many other emulators.

Have you guys a solution for that problem? Thank you very much.

Here's the 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: android.os.BinderProxy@447d2510 time:107086183
05-07 16:16:34.242: I/ActivityManager(5149): Timeline: Activity_idle id: android.os.BinderProxy@447d2510 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

Edit: That was all the logcat throws out. I just found out, that when I hold the device in landscape, it won't call onDestroy().

I implemented:

@Override
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);
}

and android:configChanges="orientation|keyboardHidden"

... but still got the same problem.

Code:

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();
        }
      }

}
هل كانت مفيدة؟

المحلول

You're calling setRequestedOrientation to landscape as soon as you start your Activity (onCreate). The documentation states

Change the desired orientation of this activity. If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted). Otherwise, this will be used the next time the activity is visible.

So... you pretty are seeing the expected behavior of your Activity.

If what you want to do is lock your screen to landscape mode, you could, for instance handle it like this:

<activity android:name="GameActivity "
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">

</activity>

which would prevent the screen configuration changes from triggering the events that you mentioned.

http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top