2016-04-04 40 views
0

當我在下面運行我的代碼時,一切正常。當我想回到之前的活動(使用模擬器)時,我會看到一個黑屏,然後應用程序關閉。當我退出應用程序並嘗試恢復時,我也會看到一個黑屏。如何導航並恢復我的應用程序而不掛上它?

代碼:

package com.example.bono.as3; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.AssetManager; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import java.io.IOException; 
import java.io.InputStream; 

public class Main extends Activity { 

    DrawView drawView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     drawView = new DrawView(this); 
     setContentView(drawView); 
    } 

    @Override public void onResume(){ 
     super.onResume(); 
     drawView.resume(); 
    } 

    @Override public void onPause(){ 
     super.onPause(); 
     drawView.pause(); 
    } 

    public class DrawView extends SurfaceView implements Runnable{ 

     Thread gameloop = new Thread(); 
     SurfaceHolder surface; 
     volatile boolean running = false; 
     AssetManager assets = null; 
     BitmapFactory.Options options = null; 
     Bitmap incect[]; 
     int frame = 0; 

     public DrawView(Context context){ 
      super(context); 
      surface = getHolder(); 
      assets = context.getAssets(); 
      options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

      incect = new Bitmap[2]; 

      try { 
       for (int n = 0; n < incect.length; n++){ 
        String filename = "incect"+Integer.toString(n+1)+".png"; 
        InputStream istream = assets.open(filename); 
        incect[n] = BitmapFactory.decodeStream(istream,null,options); 
        istream.close(); 
       } 
      } catch (IOException e){ 
       e.printStackTrace(); 
      } 
     } 

     public void resume(){ 
      running = true; 
      gameloop = new Thread(this); 
      gameloop.start(); 
     } 

     public void pause(){ 
      running = false; 
      while (true){ 
       try { 
        gameloop.join(); 
       } 
       catch (InterruptedException e){} 
      } 
     } 

     @Override public void run(){ 
      while (running){ 
       if (!surface.getSurface().isValid()) 
        continue; 
       Canvas canvas = surface.lockCanvas(); 
       canvas.drawColor(Color.rgb(85, 107, 47)); 
       canvas.drawBitmap(incect[frame], 0, 0, null); 

       surface.unlockCanvasAndPost(canvas); 

       frame++; 

       if (frame > 1) frame = 0; 

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

我不日誌中得到任何錯誤訊息,我做什麼,得到的是約13的消息說:「暫停所有線程了:X毫秒」,所以它是與我gameloop線程我認爲。不幸的是我看不出我的代碼有什麼問題...

+0

任何人都可以幫我解決這個問題嗎? – Taegos

+0

首先,當你捕捉到'InterruptedException'時'暫停',打印堆棧蹤跡,不要只是抓住它,什麼也不做。這很危險。其次,當暫停時,你讓'gameloop'等待,但是'onResume',你創建另一個新的'thread'而不處理你等待的線程。所以我想有時會暫停和恢復,你會有很多線索在等待。 –

回答

1

如果你需要一個背景下,使用getApplicationContext()或Activity.this。 (似乎與我的代碼越少越好)

回到先前使用的活動,onCreate()不會被調用,只有onResume(),所以它可能會幫助,如果你將下面的行移動到那裏。 drawView = new DrawView(this); setContentView(drawView);

  • 爲什麼要使用內部類?活動已經是一個類。如果您想多次使用該代碼,請自行創建一個類。
1

這是我用來導航回來的。也許這對你有用2!

Button backpressed = (Button) findViewById(R.id.btBack); 
    backpressed.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 

onBackPressed()只是一個方法,你可以調用,如果你想返回到最後一頁。

它看起來像什麼對我來說:

Backbutton

+0

謝謝你的回答。在我所有的其他項目中,我不需要編寫額外的代碼來導航,所以我不明白爲什麼我現在需要它。無論如何,我會嘗試你的解決方案。 – Taegos

+0

@Taegos我希望它的作品,我相對較新的Android,所以我不知道它是否在每個Android版本的作品。 – Robbert

+0

@Taegos我的awnser能幫助你嗎? – Robbert

相關問題