2011-12-29 39 views
1

我在onOptionsItemSelected()中创建了一个EditText,并试图在onClick()中获取它的信息。这里是有问题的代码:错误问题:android.widget.LinearLayout无法转换为android.widget.EditText

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); 
} 

当我还没有从项目中选择一个项目(因此没有所谓的onOptionItemSelected();)当我点击按钮,它不会崩溃。然而,当我实际上创建了EditText,并且我点击了它的按钮时它崩溃,因为它试图创建实例,给我提到了上述错误。任何想法,为什么它可以做到这一点?

编辑

这里有更多的我的代码:

@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); 
} 
+0

我认为在'onOptionItemSelected'中,创建了'mealCalories'后,你将它添加到已经在屏幕上的其他视图之一中? – 2011-12-29 22:34:08

+0

是的。它位于水平方向的LinearLayout内。 – 2011-12-30 03:49:19

+0

如果错误是LinearLayout不能转换为EditText,那么LinearLayout与EditText具有相同的ID? – 2011-12-30 07:34:09

回答

1

你似乎是使用两个不同的值:当您创建的EditText和mealCalId当你调用findViewByIdMealCalId。这是一个可能的问题。另一个是如果你有多个视图有相同的ID,findViewById不一定会返回你想要的。

编辑

乍一看,你的代码看起来像它应该工作。我不知道发生了什么问题,但我有一个解决问题的建议。当您创建的,而不是分配给它的ID视图,它分配一个标签:

mealCalories.setTag(mealCalId); 

(该int值将autoboxed到Integer。)然后在你的onClick处理程序,按标签检索:

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

如果有任何与视图ID有趣的互动,这种技术将避免它们。

如果这不起作用(或者您更喜欢),您也可以尝试使用Hierarchy Viewer诊断基于ID的检索。

+0

哦,没有两个不同的变数是我的错误。他们都是'mealCalId'。事情是这是唯一的ID(它实际上只是一个)。因为我想在不同的功能中使用相同的视图,所以我必须创建一个新的“EditText”并使其与原始的“膳食计算器”相对应。就像,我想要使用'onClick'中'onOptionItemSelected'中创建的'mealCalories'文本框。 – 2011-12-30 03:54:57

+0

@ hugo.torres我认为如果您发布了更多代码,包括将'mealCalories'添加到视图层次结构中的代码将会很有帮助。 – 2011-12-30 07:34:46

+0

@ hugo.torres - 我更新了我的答案。 – 2011-12-30 19:01:54

0

我试图运行你的代码,我发现了什么是

当菜单项没有点击,点击按钮,编辑文本为空。

所以,如果你会调用这个对象上的任何方法,它将与NullPointerException异常

崩溃当用户点击菜单项,然后点击按钮,这样你就调用这个对象上的任何方法,编辑文本不为空。

1

对于那些谁是指有和我一样的原因这个错误....

只要尝试清洁您的项目,并重新建立

为我解决了它。

相关问题