Java 错误问题:android.widget.LinearLayout无法转换为android.widget.EditText
我正在OnOptions ItemSelected()中创建一个EditText,并试图在onClick()中获取它的信息。以下是违规代码:Java 错误问题:android.widget.LinearLayout无法转换为android.widget.EditText,java,android,android-layout,android-widget,Java,Android,Android Layout,Android Widget,我正在OnOptions ItemSelected()中创建一个EditText,并试图在onClick()中获取它的信息。以下是违规代码: onOptionItemSelected(MenuItem item){ ... EditText mealCalories = new EditText(context); mealCalories.setId(MealCalId) //in this example it's just an integer 1. ... } onclick(View
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);
}
您似乎在使用两个不同的值:
MealCalId
当您创建编辑文本时,以及MealCalId
当您调用findViewById
时。这是一个可能的问题。另一个是,如果您有多个具有相同id的视图,findViewById
不一定会返回您想要的视图
编辑
乍一看,您的代码看起来应该可以工作。我不知道出了什么问题,但我有一个解决办法的建议。创建视图时,不为其指定ID,而是为其指定标记:
mealCalories.setTag(mealCalId);
(将int
值自动装箱为Integer
),然后在onClick处理程序中,按标记检索它:
EditText mealCaloriesInTextBox =
(EditText) getContentView().findViewWithTag(mealCalId);
如果与视图ID有任何有趣的交互,这种技术将避免它们
如果这不起作用(或者如果您愿意的话),您也可以尝试使用来诊断基于ID的检索。我试图运行您的代码,但我发现 未单击菜单项且单击按钮时,编辑文本为空 因此,如果您将调用此对象上的任何方法,它将因NULLPointerException而崩溃
单击菜单项,然后单击按钮时,编辑文本不为空,因此您可以调用此对象上的任何方法。对于那些由于与我相同的原因出现此错误的人 只需尝试清理项目并重新构建
为我解决了这个问题。我认为在
onOptionItemSelected
中,在您创建mealCalories
之后,您正在将它添加到屏幕上已经存在的其他视图中?是的。它位于水平方向的LinearLayout内部。如果错误是LinearLayout无法转换为EditText,那么LinearLayout与EditText具有相同的ID?LinearLayout与EditText没有相同的ID。哦,不,这两个不同的变量是我的错误。他们都是“吝啬鬼”。问题是,这是唯一具有该id的视图(实际上只是一个1)。因为我想在不同的函数中使用相同的视图,所以我必须创建一个新的“EditText”,并使其与原始的“mealCalories”相对应。比如,我想使用我在“onClick”的“onOptionItemSelected”中创建的“mealCalories”文本框。@hugo.torres我认为如果您发布更多的代码,包括将mealCalories
添加到视图层次结构中的代码,会很有帮助。