3

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:

  1. Can registerForContextMenu() wait until onFinishInflate()?
  2. If so, how do I do so?
Community
  • 1
  • 1
Android Eve
  • 14,864
  • 26
  • 71
  • 96

4 Answers4

10

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);
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • "root_element cannot be resolved or is not a field." That helped me understand that you meant that 'root_element' should be replaced by the layout root element specific in this particular exercise. +1 and accepted. :) – Android Eve Feb 24 '11 at 14:18
3

I think you should use

View v = findViewById(R.id.skeleton_activity);

instead. For the 2nd question, sorry, I 've no idea. Hope to see someone else's answer.

Huang
  • 4,812
  • 3
  • 21
  • 20
2

You shouldn't need to wait for the content to inflate in an Activity.

One problem is that findViewById takes an ID (R.id....) when you provide it with a layout (R.layout...). Can you try the following instead, to reference the Activity's root view?

setContentView(R.layout.skeleton_activity);    
View content = findViewById(android.R.id.content);
registerForContextMenu(content);
Romain
  • 2,318
  • 1
  • 23
  • 31
  • Thanks and +1. Interestingly, 'R.id.content' doesn't work whereas 'android.R.id.content' works. Why? – Android Eve Feb 24 '11 at 13:43
  • OK, I found the answer. 'content' wasn't defined in the (single) layout XML file. It was 'editor' that was defined as the root element. – Android Eve Feb 24 '11 at 14:16
  • 1
    No worries. android.R.id.content points to your root element without you having to manually give it an ID. – Romain Feb 24 '11 at 21:22
  • 1
    Oh, and you have to specify the "android" namespace because this special ID is defined in Android's own "R" class. If you don't specify the namespace Java will look in your app's "R" class instead... and won't find it :) – Romain Feb 24 '11 at 21:22
0

i think the code you have shown is very confusing. Here is a good article http://blog.sptechnolab.com/2011/02/10/android/android-contextmenu-submenu/. In my case it works, i hope you can solve your problem.

user609239
  • 3,356
  • 5
  • 25
  • 26