在我的活动中,我有一些观点和一个surfaceView
.
这是我的第一个代码onCreate()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
upBtn = (Button) findViewById(R.id.upBtn);
// and more widget here
surface = (SurfaceView) findViewById(R.id.surfaceView);
this.holder = surface.getHolder(); // NullPointerException
setContentView(R.layout.view);
如果我通过取surfaceView
并更改上面getHolder()
的代码onResume()
(我已经尝试了很多次得到这个结果),没有错误:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
upBtn = (Button) findViewById(R.id.upBtn);
// and more widget here
// surface = (SurfaceView) findViewById(R.id.surfaceView); //cancel
// this.holder = surface.getHolder(); // cancel
setContentView(R.layout.view);
public void onResume() {
surface = (SurfaceView) findViewById(R.id.surfaceView);
this.holder = surface.getHolder(); // No Error now
请为我解释。