0

我有一个绘制到画布上的 android 应用程序。到目前为止,我每次使用 onCreate 创建应用程序时都在绘制:

package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class DrawDemo extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }
}

但是我想将绘图迁移到 onResume。理想情况下,我不会在某人第一次启动程序时绘制,只有在他们恢复程序时才绘制。为什么以下两个代码片段不起作用:?

package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class DrawDemo extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }

    @Override
    public void onResume(){
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }
}

package com.example.drawdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class DrawDemo extends Activity {
    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onResume(){
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }
}
4

1 回答 1

0

确保您致电super.onResume()- 如果没有它,您的活动将无法正常运行。

于 2013-03-08T23:22:55.523 回答