【问题标题】:Problems with error: android.widget.LinearLayout cannot be cast to android.widget.EditText错误问题:android.widget.LinearLayout 无法转换为 android.widget.EditText
【发布时间】:2011-12-29 22:24:05
【问题描述】:

我正在 onOptionsItemSelected() 中创建一个 EditText,并尝试在 onClick() 中获取它的信息。这是有问题的代码:

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

【问题讨论】:

  • 我认为在onOptionItemSelected 中,在您创建mealCalories 之后,您要将其添加到屏幕上已经存在的其他视图之一?
  • 是的。它位于水平方向的 LinearLayout 内。
  • 如果报错是无法将LinearLayout转换为EditText,那么LinearLayout的ID与EditText相同?
  • LinearLayout 的 ID 与 EditText 不同。

标签: java android android-layout android-widget


【解决方案1】:

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

编辑

乍一看,您的代码看起来应该可以工作。我不知道出了什么问题,但我有一个解决方法的建议。创建视图时,不要为其分配 ID,而是为其分配一个标签:

mealCalories.setTag(mealCalId);

int 值将自动装箱为 Integer。)然后在您的 onClick 处理程序中,通过标记检索它:

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

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

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

【讨论】:

  • 哦,不,这两个不同的变量是我的错误。他们都是'mealCalId'。问题是这是唯一具有该 id 的视图(它实际上只是一个 1)。因为我想在不同的函数中使用相同的视图,所以我必须创建一个新的“EditText”并使其与原始的“mealCalories”相对应。比如,我想使用我在 'onClick' 中的 'onOptionItemSelected' 中创建的 'mealCalories' 文本框。
  • @hugo.torres 我认为如果您发布更多代码会有所帮助,包括将mealCalories 添加到视图层次结构的代码。
【解决方案2】:

对于那些因与我相同的原因而出现此错误的人......

尝试清理您的项目并重新构建

帮我解决了。

【讨论】:

    【解决方案3】:

    我试图运行你的代码,我发现是这样的

    未点击菜单项且点击按钮时,编辑文本为空。

    所以如果你调用这个对象的任何方法,它都会崩溃并出现 NULLPointerException

    单击菜单项然后单击按钮时,编辑文本不为空,因此您可以调用此对象的任何方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      • 2020-08-22
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      相关资源
      最近更新 更多