Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在不使用布局充气器的情况下使用getView()_Android_Android Layout_Android Edittext_Classcastexception_Android Adapter - Fatal编程技术网

Android 在不使用布局充气器的情况下使用getView()

Android 在不使用布局充气器的情况下使用getView(),android,android-layout,android-edittext,classcastexception,android-adapter,Android,Android Layout,Android Edittext,Classcastexception,Android Adapter,我希望在不使用xml布局的情况下填充项目,而希望在运行时创建视图。这是我的getView()方法 public View getView(final int position, View convertView, ViewGroup parent) { EditText v ; if(convertView ==null) { v = new EditText(mContext); convertView = v;

我希望在不使用xml布局的情况下填充项目,而希望在运行时创建视图。这是我的
getView()
方法

 public View getView(final int position, View convertView, ViewGroup parent) {

    EditText v ;    

    if(convertView ==null)
    {
        v = new EditText(mContext);
        convertView = v;
        convertView.setTag(R.string.edittext_child, v);

    }
    else
    {
        v = (EditText)convertView.getTag(R.string.edittext_child);
    }
    v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 80));
    v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.custom_edit));
    v.setEms(12);
    v.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    v.setPadding(10, 0, 5, 0);
    v.setHint(Ids[position]);
    return convertView;
}
返回视图时出现此错误

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

现在很可能导入了LinearLayout布局参数,这是一个错误的导入。 这就是为什么你会

java.lang.ClassCastException:android.view.ViewGroup$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams

 v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 80));
因为您正在尝试将线性布局参数添加到AbsList布局,该布局应使用AbdList参数填充

这一行是错误的:

v、 setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,80))

将LayoutParams更改为:

AbsListView.LayoutParams
或更改您的导入:

import android.widget.AbsListView;

使用AbsListView.LayoutParams

 v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 80));

您可以将
编辑文本
视为
视图
,但不能反过来。我认为您需要使用LinearLayout.LayoutParams而不是ViewGroup.LayoutParams删除LayoutParams的导入行并导入LinearLayout.LayoutParams。它应该会起作用。