gpt4 book ai didi

android - ImageView setVisibility(0) 和空指针异常

转载 作者:行者123 更新时间:2023-11-29 14:33:38 24 4
gpt4 key购买 nike

我有 2 个 xml 文件,一个主文件中有一个 listView,另一个是我对 listView 进行一些自定义的地方.第二个 xml 用于我的自定义 ArrayAdapter

在第二个 xml 中,我有一个 ImageView,我想立即将其设置为可见,但我收到了 空指针异常 因为我从来没有用 setCurrentView 设置第二个 xml 文件,我的 ImageView 在哪里。

我正在尝试这样做:

ImageView statusOk=(ImageView)findViewById(R.id.statusOkImage);
statusOk.setVisibility(0);

你能告诉我如何解决这个问题吗?

谢谢。

编辑:

这里是更多的代码:

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
android:id="@+id/ListView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>

第二个 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
android:id="@+id/statusOkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/status_ok"
android:visibility="invisible"/>

<TextView android:id="@+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

主要 Activity :

public class TestListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}

private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();

SearchResults sr1 = new SearchResults();
sr1 = new SearchResults();
sr1.setName("Fred Jones");
sr1.setCityState("Las Vegas, NV");
sr1.setPhone("612-555-8214");
results.add(sr1);
return results;
}
}

自定义适配器:

public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;

private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}

public int getCount() {
return searchArrayList.size();
}

public Object getItem(int position) {
return searchArrayList.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);


convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

mInflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

View view=mInflater.inflate(R.layout.custom_row_view, null);

ImageView statusOk=(ImageView)view.findViewById(R.id.imageView1);
statusOk.setVisibility(View.VISIBLE);
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
holder.txtPhone.setText(searchArrayList.get(position).getPhone());

return convertView;
}

static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
}
}

最后是 listView 的资源类

public class SearchResults {
private String name = "";
private String cityState = "";
private String phone = "";

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setCityState(String cityState) {
this.cityState = cityState;
}

public String getCityState() {
return cityState;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getPhone() {
return phone;
}
}

最佳答案

要设置在 xml 中定义的可见性 View ,您需要在 Activity 中膨胀该 View ,setContentView 会自动执行它,并且由于您没有在第二个 xml 上调用 setContentView,然后使用:

LayoutInflater inflater=(LayoutInflater)getSystemService(Layout_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.secondxml, null);

然后从那个 View 中找到你的 View :

ImageView statusOk=(ImageView)view.findViewById(R.id.statusOkImage);
statusOk.setVisibility(0);

但它只适用于 View 对象,不会影响原始 xml。

关于android - ImageView setVisibility(0) 和空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9311260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com