1

I'm creating an EditText in onOptionsItemSelected() and trying to get it's information in onClick(). Here's the offending code:

onOptionItemSelected(MenuItem item){
...
EditText mealCalories = new EditText(context);
mealCalories.setId(MealCalId) //in this example it's just an integer 1.
...
}

onclick(View v){
EditText mealCaloriesInBox = (EditText)findViewById(mealCalId);
}

When I haven't selected an item from the menu (and thus haven't called onOptionItemSelected();) it doesn't crash when I click the button. However, when I actually have created the EditText and I click the button it crashes as it's trying to create the instance, giving me the aforementioned error. Any ideas on why it could be doing that?

EDIT

Here's more of my code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    Context context = getApplicationContext();

    switch(item.getItemId()) {
    case R.id.addMeal:

        trackMealItems++;
        mealCalId++;
        mealFatId++;
        mealCarbId++;
        mealProteinId++;

        //the base layout
        LinearLayout root = (LinearLayout)findViewById(R.id.linearLayout1);

        //make the layout that holds the meal item  and add it to the base layout
        LinearLayout mealItem = new LinearLayout(context);
        mealItem.setId(trackMealItems);
        mealItem.setOrientation(LinearLayout.VERTICAL);
        mealItem.setLayoutParams(mealItemParams);   
        root.addView(mealItem);

        //make the TextView that holds the name of the meal and add it to the mealItem layout
        EditText mealName = new EditText(context);
        mealName.setLayoutParams(mealNameParams);
        mealItem.addView(mealName);

        //make the TextViews that hold the information about the meal and stick them in a 
        //horizontal LinearLayout
        LinearLayout mealStats = new LinearLayout(context);
        mealStats.setOrientation(LinearLayout.HORIZONTAL);
        mealStats.setLayoutParams(mealStatParams);
        mealItem.addView(mealStats);

        EditText mealCalories = new EditText(context);
        mealCalories.setId(mealCalId);
        mealCalories.setLayoutParams(mealStatParams);
        mealStats.addView(mealCalories);

        EditText mealFat = new EditText(context);
        mealFat.setId(mealFatId);
        mealFat.setLayoutParams(mealStatParams);
        mealStats.addView(mealFat);

        EditText mealCarbs = new EditText(context);
        mealCarbs.setId(mealCarbId);
        mealCarbs.setLayoutParams(mealStatParams);
        mealStats.addView(mealCarbs);

        EditText mealProtein = new EditText(context);
        mealProtein.setId(mealProteinId);
        mealProtein.setLayoutParams(mealStatParams);
        mealStats.addView(mealProtein);

        return true;


    case R.id.removeMeal:
        LinearLayout removeMe = (LinearLayout)findViewById(trackMealItems);
        removeMe.setVisibility(View.GONE);

        trackMealItems--;

        return true;
    }


    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v){
    EditText mealCaloriesInTextBox = (EditText)findViewById(mealCalId);
}
4
  • I take it that in onOptionItemSelected, after you've created mealCalories, you're adding it to one of the other views already on the screen? Commented Dec 29, 2011 at 22:34
  • Yeah. It's inside a horizontally oriented LinearLayout. Commented Dec 30, 2011 at 3:49
  • If the error is that the LinearLayout cannot be cast to EditText, then the LinearLayout has the same ID as the EditText? Commented Dec 30, 2011 at 7:34
  • The LinearLayout doesn't have the same ID as the EditText. Commented Dec 31, 2011 at 6:27

3 Answers 3

1

You seem to be using two different values: MealCalId when you create your EditText and mealCalId when you call findViewById. That's one possible problem. The other is that if you have more than one view with the same id, findViewById will not necessarily return the one you want.

EDIT

At first glance, your code looks like it should work. I don't know what's going wrong, but I have a suggestion for a work-around. When you create the view, instead of assigning it an ID, assign it a tag:

mealCalories.setTag(mealCalId);

(The int value will be autoboxed to an Integer.) Then in your onClick handler, retrieve it by tag:

EditText mealCaloriesInTextBox =
    (EditText) getContentView().findViewWithTag(mealCalId);

If there's any kind of funny interaction with view IDs, this technique will avoid them.

If that doesn't work (or if you prefer anyway) you can also try diagnosing the ID-based retrieval using the Hierarchy Viewer.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh no the two different variables were my mistake. They are both 'mealCalId'. The thing is that's the only view with that id (it's actually just a 1). Because I want to use that same view in a different function, I have to create a new 'EditText' and make it correspond to the original 'mealCalories'. Like, I want to use the 'mealCalories' text box that I create in 'onOptionItemSelected' in 'onClick'.
@hugo.torres I think it would be helpful if you posted more of your code, including the code that adds mealCalories to the view hierarchy.
1

For those who are having this error for the same reason I did....

Just try cleaning your project and re-building.

Solved it for me.

Comments

0

i tried to run your code and what i found is that

when menu item is not clicked and button is clicked, the edit text is null.

So if you will call any method on this object, it will crash with NULLPointerException

When menu item is clicked and then button is clicked, the edit text is not null so you call any method on this object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.