Android 从listview OnClickListener返回null的findViewById
我有一个activity_main.xml,其中包含一个列表视图-Android 从listview OnClickListener返回null的findViewById,android,Android,我有一个activity_main.xml,其中包含一个列表视图- <EditText android:id="@+id/phoneText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType=
<EditText
android:id="@+id/phoneText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone">
</EditText>
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp">
</ListView>
但是findViewById返回null。请注意,用于填充listview和editRecordHandler的代码位于不同于MainActivity.java的类中
另外,显然这不是一篇重复的文章,因为我已经扫描了所有现有的文章,只发现了一篇相关的,但不确定解决方案
DBAdapter的完整代码
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
public class DBAdapter extends BaseAdapter {
private DBHelper dbHelper = null;
public DBAdapter(DBHelper dbHelper) {
this.dbHelper = dbHelper;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int index) {
return null;
}
@Override
public long getItemId(int index) {
return 1;
}
@Override
public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.list_view_item , parent, false);
}
ImageView deleteView = (ImageView) view.findViewById(R.id.deleteButton);
deleteView.setOnClickListener(deleteRecordHandler);
deleteView.setTag(contact.id);
ImageView editView = (ImageView) view.findViewById(R.id.editButton);
editView.setOnClickListener(editRecordHandler);
editView.setTag(contact.id);
return view;
}
View.OnClickListener editRecordHandler = new View.OnClickListener() {
public void onClick(View v) {
TextView phone = (TextView) v.findViewById(R.id.phoneText);
phone.setText("ABC");
}
};
}
下面是List_view_Item.xml
<LinearLayout>
<ImageView
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/delete_button"
android:layout_marginLeft="10dp"
android:paddingTop="15dp"
android:src="@drawable/ic_del_unselected" />
<ImageView
android:id="@+id/editButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/delete_button"
android:layout_marginLeft="10dp"
android:paddingTop="15dp"
android:src="@drawable/ic_edit" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:orientation="vertical" >
<TextView
android:id="@+id/nameDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:textStyle="bold"
android:textSize="16sp" />
<TextView
android:id="@+id/modeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
完整编辑:
首先,您不是在更改TextView文本,而是在更改EditText文本,对吗??因为id phoneText用于EditText。
其次,要在listview行之外获取视图,请执行以下操作:
@Override
public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.list_view_item , parent, false);
}
final OnClickListener editRecordHandler = new OnClickListener() {
@override
public void onClick(View v) {
View v2 = parent.getRootView();
TextView phone = (TextView) v2.findViewById(R.id.phoneText);
phone.setText("ABC");
}
}
ImageView deleteView = (ImageView) view.findViewById(R.id.deleteButton);
deleteView.setOnClickListener(deleteRecordHandler);
deleteView.setTag(contact.id);
ImageView editView = (ImageView) view.findViewById(R.id.editButton);
editView.setOnClickListener(editRecordHandler);
editView.setTag(contact.id);
return view;
}
如果视图的id匹配,View.findViewById将返回该值,否则返回false。ViewGroup.findViewById在其子项上循环,以在其中查找与您提供的id匹配的项。因此,如果您在按钮上设置了onClick Listener,并且在onClick中尝试查找EditText或其他不是您单击的组件,您将始终得到null,但是如何才能在不同的类中查找phoneText??OP所说的请注意,填充listview和editRecordHandler的代码位于不同于MainActivity.javaFindViewById的类中。它说这种方法不被认可。请注意,我是从一个适配器内部使用它的,即一个扩展BaseAdapterPost适配器代码的类。TextView应该来自AdapterHanks内部膨胀的视图…但情况是一样的:。我仍然得到NullPointer异常。你从哪里得到它?在最终的TextView phone=TextView view.findviewbydr.id.phoneText;?如果是,则视图为空。检查您的xml layout.list\u view\u项目或将其发布到此处,以便我们检查您在何处设置了imageView onClickListener?您必须在适配器内部设置它。你能展示一下你的适配器代码吗?用代码更新好的…那么解决办法是什么?有没有像getParentView这样的东西?
@Override
public View getView(int index, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.list_view_item , parent, false);
}
final OnClickListener editRecordHandler = new OnClickListener() {
@override
public void onClick(View v) {
View v2 = parent.getRootView();
TextView phone = (TextView) v2.findViewById(R.id.phoneText);
phone.setText("ABC");
}
}
ImageView deleteView = (ImageView) view.findViewById(R.id.deleteButton);
deleteView.setOnClickListener(deleteRecordHandler);
deleteView.setTag(contact.id);
ImageView editView = (ImageView) view.findViewById(R.id.editButton);
editView.setOnClickListener(editRecordHandler);
editView.setTag(contact.id);
return view;
}