简体   繁体   English

Android:surfaceView无法在onCreate()中获取getHolder(),而必须在onResume()中

[英]Android : surfaceView cannot getHolder() in onCreate(), must in onResume()

In my activity, I has some views and a surfaceView . 在我的活动中,我有一些视图和surfaceView

Here is my first code in onCreate() 这是我在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);

If I change the above code by taking surfaceView and getHolder() in onResume() (I have tried many times to have this result), no error: 如果我通过在onResume() surfaceViewgetHolder()来更改上述代码(我已经尝试了很多次以获得此结果),则不会出现错误:

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

Please explain for me. 请给我解释一下。

Becaues you need to call 由于您需要致电

setContentView(R.layout.layoutXML); 

before you call findViewById(R.id.viewID) . 在调用findViewById(R.id.viewID) that is why your findview by id returning nul, and you probably getting nullPointerException when you are calling surfaceView.getHolder 这就是为什么id通过id返回nul的findview的原因,并且在调用surfaceView.getHolder时可能会得到nullPointerException

Suggested solution is, Create a layout.xml file which should have surfaceView inside any layout. 建议的解决方案是,创建一个layout.xml文件,该文件的任何布局内均应具有surfaceView。

in your Activity's oncreate call setContentView(R.layout.layoutXML); 在您的Activity的oncreate调用setContentView(R.layout.layoutXML); and after this you can retrieve surface view by findViewByID and do whatever you want with it. 然后,您可以通过findViewByID检索表面视图,并对其进行任何操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在空对象引用上调用虚方法'android.view.SurfaceHolder android.view.SurfaceView.getHolder()' - Attempt to invoke virtual method 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()' on a null object reference Android在onCreate中保存微调器状态,并在onResume中保存活动 - Android Save spinner state in onCreate, and Activity onResume Android Studio活动-使用onCreate(),onResume()等 - Android Studio activity - using onCreate(), onResume() etc Android - 如何访问在 onResume 中的 onCreate 中实例化的 View 对象? - Android - How can I access a View object instantiated in onCreate in onResume? 后台服务在 android 中的 onCreate() 和 onResume() 中随机停止工作 - Background service stops working randomly in android in onCreate() and onResume() 该方法在OnResume()中起作用,但在OnCreate()中不起作用 - Method works in OnResume() but not in OnCreate() 在调用onResume()之后刷新onCreate() - refresh onCreate() after call to onResume() Android:getHolder().setFixedSize(1920,1080) 导致屏幕分辨率问题 - Android: getHolder().setFixedSize(1920,1080) causes Issue with screen Resolution 在onCreate和onResume之间将MPAndroidchart mChart设置为null - MPAndroidchart mChart set to null between onCreate and onResume 在onCreate中初始化变量,但在onResume()中对其进行更新 - Initializing variables in onCreate, but updating them in onResume()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM