findViewByID(R.layout.skeleton_activity)返回null [英] findViewByID(R.layout.skeleton_activity) returns null
问题描述
我想在一个骨架应用程序的OnCreate()注册的上下文菜单:
/ **与第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //从XML布局描述充气我们的UI。
的setContentView(R.layout.skeleton_activity); 视图V = findViewById(R.layout.skeleton_activity);
registerForContextMenu(五); //查找布局中的文本编辑器视图,因为我们
//想要做的各种事情编程用它。
mEditor =(EditText上)findViewById(R.id.editor); //挂钩按钮presses到相应的事件处理程序。
((按钮)findViewById(R.id.back))setOnClickListener(mBackListener)。
((按钮)findViewById(R.id.clear))setOnClickListener(mClearListener)。 mEditor.setText(的getText(R.string.main_label));
}
调试器告诉我,findViewById(R.layout.skeleton_activity)返回null。
@CommonsWare解决了类似的帖子是要等到onFinishInflate()。然而,在<一href=\"https://github.com/commonsguy/cw-advandroid/blob/master/Animation/SlidingPanel/src/com/commonsware/android/anim/SlidingPanelDemo.java\"相对=nofollow>示例项目他提供了,它似乎并不认为他会等到onFinishInflate。
我的问题:
- 可以registerForContextMenu()等
直到onFinishInflate()? - 如果是这样,我怎么办呢?
这行没有纠正其要求ID和你提供的布局
视图V = findViewById(R.layout.skeleton_activity);
相反,如果你想有你的根布局元素的对象,然后提供它的一些ID,然后尝试这样的事情
视图V = findViewById(R.id.root_element);
I am trying to register a context menu in a skeleton app's 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));
}
The debugger tells me that findViewById(R.layout.skeleton_activity) returns null.
@CommonsWare solution to a similar post is to Wait until onFinishInflate(). However, in the sample project he provides, it doesn't seem that he waits until onFinishInflate.
My questions:
- Can registerForContextMenu() wait until onFinishInflate()?
- If so, how do I do so?
This line is not correct its asking for id and you are providing layout
View v = findViewById(R.layout.skeleton_activity);
Instead if you want to have object of your root layout element then provide it some id and then try something like this
View v = findViewById(R.id.root_element);
这篇关于findViewByID(R.layout.skeleton_activity)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!