Android ListView正在显示,但应用程序在滚动和单击时崩溃
我有一个名为custom\ucode>ListView.xml的自定义布局的Android ListView正在显示,但应用程序在滚动和单击时崩溃,android,listview,android-listview,android-scrolling,Android,Listview,Android Listview,Android Scrolling,我有一个名为custom\ucode>ListView.xml的自定义布局的ListView: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView xmlns
ListView
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:id="@+id/file_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/file_path"/>
</LinearLayout>
这是我的onClickListener
,用于ListView
:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int color = Color.TRANSPARENT;
Drawable background = parent.getChildAt(position).getBackground();
if (background instanceof ColorDrawable)
color = ((ColorDrawable) background).getColor();
if(color == Color.CYAN)
{ parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);}
else {
parent.getChildAt(position).setBackgroundColor(Color.CYAN);
TextView t;
t = (TextView) view.findViewById(R.id.file_path);
files_selected.addElement(t.getText().toString());
}
}
}
public void onItemClick(AdapterView父对象、视图视图、int位置、长id){
int color=color.TRANSPARENT;
可绘制背景=parent.getChildAt(position.getBackground();
if(可着色的背景实例)
颜色=((可着色)背景);
if(color==color.CYAN)
{parent.getChildAt(position.setBackgroundColor(Color.TRANSPARENT);}
否则{
父.getChildAt(位置).setBackgroundColor(颜色.CYAN);
文本视图t;
t=(TextView)view.findViewById(R.id.file\u路径);
文件_selected.addElement(t.getText().toString());
}
}
}
当我点击列表顶部的任何项目时,该应用程序工作正常。但只要我向下滚动并单击任何项目,应用程序就会崩溃。请帮忙。谢谢 你忘了别的。试试这个。若你们并没有实现else部分,那个么当你们滚动你们的应用程序时就会被折叠
Holder holder;
if (convertView == null)
{
holder=new Holder();
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_listview, null);
holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
编辑:如果您设置了If条件If(convertView==null)
并忘记设置else部分,则回收时间(滚动)将尝试重新创建,行与行之间的文本更改、崩溃等都会发生。所以,无论何时在getView中添加if条件,都必须添加if和else
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_listview, null);
Holder holder=new Holder();
holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
//holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.file_name.setText(result.get(position).getName());
holder.file_path.setText(result.get(position).getPath());
}else{
viewHolder = (ViewHolder) view.getTag();
}
view.setTag(viewHolder);
为什么它会崩溃?它说的是viewHolder无法解决。还有,我应该从getView()方法返回什么?哦,很抱歉,您需要更改holder=(ViewHolder)convertView.getTag();这个to holder=(holder)convertView.getTag();检查我的编辑并尝试我的最后评论。它会工作的。现在它是完美的滚动,但它崩溃时,我向下滚动,并点击任何项目。到底是什么问题?谢谢你的帮助。
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.custom_listview, null);
Holder holder=new Holder();
holder.file_name=(TextView) convertView.findViewById(R.id.file_name);
holder.file_path=(TextView) convertView.findViewById(R.id.file_path);
//holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.file_name.setText(result.get(position).getName());
holder.file_path.setText(result.get(position).getPath());
}else{
viewHolder = (ViewHolder) view.getTag();
}
view.setTag(viewHolder);