findViewByID(R.layout.skeleton_activity)返回null

And*_*Eve 2 android

我想在骨架应用程序的OnCreate()中注册一个上下文菜单:

/** Called with the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate our UI from its XML layout description.
    setContentView(R.layout.skeleton_activity);

    View v = findViewById(R.layout.skeleton_activity);
    registerForContextMenu(v);      

    // Find the text editor view inside the layout, because we
    // want to do various programmatic things with it.
    mEditor = (EditText) findViewById(R.id.editor);

    // Hook up button presses to the appropriate event handler.
    ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
    ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);

    mEditor.setText(getText(R.string.main_label));
}
Run Code Online (Sandbox Code Playgroud)

调试器告诉我findViewById(R.layout.skeleton_activity)返回null.

@CommonsWare对类似帖子的解决方案是等到onFinishInflate().但是,在他提供的示例项目中,他似乎并不等到onFinishInflate.

我的问题:

  1. registerForContextMenu()可以等到onFinishInflate()吗?
  2. 如果是这样,我该怎么办?

ing*_*abh 10

这条线是不正确的,它要求id,你提供布局

View v = findViewById(R.layout.skeleton_activity);
Run Code Online (Sandbox Code Playgroud)

相反,如果你想拥有你的根布局元素的对象,然后提供一些id,然后尝试这样的事情

View v = findViewById(R.id.root_element);
Run Code Online (Sandbox Code Playgroud)