android:app停止然后在屏幕熄灭时崩溃

时间:2014-12-14 21:11:44

标签: android

该应用程序运行完美,但是当我的智能手机屏幕在一段时间不活动后熄灭时,应用程序就会停止运行。当我按下按钮返回手机的主菜单时,它就会崩溃(停止)。

如果屏幕没有关闭,或者应用程序继续运行,我想要的是,如果应用程序在屏幕关闭时退出,那就没问题。我只是不知道如何。

非常感谢。

logcat

12-14 21:47:07.526: E/AndroidRuntime(1111): FATAL EXCEPTION: main
12-14 21:47:07.526: E/AndroidRuntime(1111): Process: com.IceDriftGFS.icedrift, PID: 1111
12-14 21:47:07.526: E/AndroidRuntime(1111): java.lang.RuntimeException: Unable to pause activity {com.IceDriftGFS.icedrift/com.IceDriftGFS.icedrift.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Thread.join()' on a null object reference
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3225)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3184)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3159)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.access$1000(ActivityThread.java:144)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.os.Looper.loop(Looper.java:135)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invoke(Native Method)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at java.lang.reflect.Method.invoke(Method.java:372)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-14 21:47:07.526: E/AndroidRuntime(1111): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.lang.Thread.join()' on a null object reference
12-14 21:47:07.526: E/AndroidRuntime(1111):     at com.IceDriftGFS.icedrift.Main$Run.pause(Main.java:114)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at com.IceDriftGFS.icedrift.Main.onPause(Main.java:54)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.Activity.performPause(Activity.java:6044)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1294)
12-14 21:47:07.526: E/AndroidRuntime(1111):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3211)
12-14 21:47:07.526: E/AndroidRuntime(1111):     ... 11 more

代码:

@SuppressLint("ClickableViewAccessibility") 
public class Main extends Activity implements OnTouchListener{

    Run run;
    static float Xpos, Yinc;
    static boolean actionDown = false;
    static int x, y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);

        run = new Run(this);
        run.setOnTouchListener(this);               
        setContentView(run);
    }

    @Override
    protected void onResume(){
        super.onResume();
        run.resume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        run.pause();
    }

public class Run extends SurfaceView implements Runnable{

    SurfaceHolder surfaceHolder;
    Canvas canvas;
    Thread thread = null;
    boolean isRunning = true;

    DisplayMetrics display;

    Player player;
    Background background;
    Walls walls;


    boolean plusCount = false;

    public Run(Context context) {
        super(context);
        surfaceHolder = getHolder();
        thread = new Thread(this);
        thread.start();

        display = context.getResources().getDisplayMetrics();

        if(display.widthPixels > display.heightPixels){
            Toast.makeText(context, "Your device is not compatible with this game", Toast.LENGTH_LONG).show();
        }

        player = new Player(context, display.widthPixels, display.heightPixels);
        background = new Background(context, display.widthPixels, display.heightPixels);
        walls = new Walls(context, display.widthPixels, display.heightPixels);


        Xpos = display.widthPixels - display.widthPixels/6;

        Yinc = display.heightPixels/280;
    }   

    public void pause(){
        isRunning = false;
        while(true){
            try{
                thread.join();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            break;
        }
        thread = null;
    }   

    public void resume(){
        isRunning = true;
    }   

    @Override
    public void run() {
        while(isRunning){   
            if(!surfaceHolder.getSurface().isValid())
                continue;
            canvas = surfaceHolder.lockCanvas();

            try {
                Thread.sleep((long) 16.6666666667);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            background.run(canvas, Yinc);
            player.run(canvas, Xpos, actionDown);
            walls.run(canvas, Yinc);



            surfaceHolder.unlockCanvasAndPost(canvas);          
        }
    }



}

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        x = (int) event.getX();
        y = (int) event.getY();

        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                actionDown = true;
                Xpos = event.getX();    
            break;

            case MotionEvent.ACTION_UP:
                actionDown = false;
            break;
        }   
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

要使屏幕始终打开,您可以锁定他。 把它放在AndroidManifest.xml中:

<uses-permission android:name="android.permission.WAKE_LOCK" />

这会阻止屏幕熄灭。

关于错误,你能让我们看看logCat,我们将能够看到错误或代码,我们将能够理解发生的事情。

编辑:现在我们可以看到代码和错误,我们可以看到你有NullPointerException。 我认为你应该添加if(thread!= null)并且thrad.join()将在他内部。 如果有效,请告诉我:))