setContentView设置显示黑色

时间:2018-05-04 17:12:12

标签: java android setcontentview

会有很多代码。我不得不留下它来了解应用程序的逻辑。 这是MainActivity。被称为开始。

public class MainActivity extends AppCompatActivity {

    private GameView gameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //some unnecessary code
        setContentView(R.layout.activity_main);
    }

    public void startSurvival(View view) {
        gameView = new GameView(this, this, "survival");
        setContentView(gameView);
    }

    public void chooseData(View view){
        setContentView(new DView(this, this));
    }

    public void backToMenu(){
        setContentView(R.layout.activity_main);
        gameView = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            gameView.update();
        } catch (NullPointerException e) {}
    }
}

此活动是一个选项列表。您选择一个,然后使用适当的参数将GameView设置为内容视图。 这里没有问题,所以我几乎删除了所有代码。

public class DView extends ListView {

    DView(final Context context, final MainActivity mainActivity){
        super(context);

        this.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String[] columns = {"data"};
                String having = "id = " + ids[position];

                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("levels", columns, null, null, ID, having, null);

                if (cursor.moveToFirst()){
                    int dataInd = cursor.getColumnIndex(DATA);
                    mainActivity.setContentView(new GameView(context, mainActivity, cursor.getString(dataInd)));
                }//everything here works fine. This just shows that setContentView can be done multiple times 
        //without bugs 
                cursor.close();
                dbHelper.close();

            }
        });
    }
}

这就是问题所在。当调用win()方法时,显示变为黑色。应用程序不会崩溃。

public class GameView extends SurfaceView{

    public MainActivity mainActivity;
    GameThread gameThread;
    public Player player = null;
    public Canvas canvas;
    public ExtraData data;

    public GameView (Context context, MainActivity mainActivity, String data){
        super(context);
        this.mainActivity = mainActivity;
        if (data.equals("survival")) {
            this.data = new ExtraData("RandomSpawn47",null, this);
        } else {
            this.data = new ExtraData("UsingData", data, this);
        }
        update();
    }

    void update(){
        gameThread = new GameThread(this);
        getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                gameThread.running(true);
                if (gameThread.getState() == Thread.State.NEW)
                    gameThread.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                gameThread.running(false);
            }
        });
        if (player == null)
            player = new Player(this);
    }

    public class GameThread extends Thread{
        private GameView gameView;

        public GameThread(GameView gameView) {
            this.gameView = gameView;
        }
            public void running(boolean run){
                running = run;
        }

        @Override
        public void run() {
            while (running){
                 canvas = null;
                try{
                    canvas = gameView.getHolder().lockCanvas(null);
                    synchronized (gameView.getHolder()){
                        draw(canvas);
                        this.wait(45);
                    }
                } catch(Exception e) {}
                finally {
                    if((canvas != null)&&(running)){
                        gameView.getHolder().unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        canvas.drawColor(Color.BLUE);
        data.onDraw();
    }

    public void win(){
        mainActivity.backToMenu();//not switching the menu
    }
}

ExtraData和Player等其他类并不重要。

GameThread和SurfaceView破坏(我在onDestroy()和run()方法结束时使用Logs检查它们)。

1 个答案:

答案 0 :(得分:0)

您正在从另一个类调用Activity的方法。而不是mainActivity.backToMenu(),创建一个接口,在MainActivity中实现它并在GameView中传递引用以初始化接口。在调用win方法的地方,调用接口方法而不是调用MainActivity的公共方法。

创建一个界面,如:

public interface UpdateActivity{
    void updateActivity();
}

在MainActivity中

public class MainActivity extends AppCompatActivity implements UpdateActivity

然后覆盖接口

的方法
void updateActivity(){
        setContentView(R.layout.activity_main);
        gameView = null;
}

在GameView中传递引用,而不是传递MainActivity引用。

    public GameView (Context context, UpdateActivity updateActivity , String data) {
        super(context);
        this.updateActivity = updateActivity ;
        if (data.equals("survival")) {
            this.data = new ExtraData("RandomSpawn47",null, this);
        } else {
            this.data = new ExtraData("UsingData", data, this);
        }
        update();
    }

现在调用您要调用的方法:

updateActivity.updateActivity();