ImageView setVisibility(0)和空指针异常

时间:2012-02-16 12:27:54

标签: android android-view

我有2个xml个文件,一个主文件,我有一个listView,另一个我正在为listView做一些自定义。 第二个xml用于我的自定义ArrayAdapter

在第二个xml我有一个ImageView我希望在某个时刻设置为可见但我得到的是null pointer exception因为我从未设置setCurrentView第二个xml文件在哪里是我的ImageView

我正在尝试这样做:

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

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

谢谢。

编辑:

这是更多代码:

main.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>

MainActivity:

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

5 个答案:

答案 0 :(得分:4)

要设置在xml中定义的可见性视图,您需要在activity中扩充该视图,setContentView自动执行此操作,并且当您没有在第二个xml上调用setContentView时,请使用:

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

然后从该视图中找到您的观点:

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

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

答案 1 :(得分:2)

更改为:

statusOk.setVisibility(View.INVISIBLE);

并检查图像是否已准备好使用findViewById,就像调用方法一样,然后执行setContentView,或者它是否真的是ImageView等。

答案 2 :(得分:2)

以下是从另一个类访问视图的解决方案。 注意:currentActivity是根类的上下文

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

答案 3 :(得分:0)

使用LayoutInflater.getLayoutInflater()将布局XML文件实例化为相应的View对象。

答案 4 :(得分:0)

ImageView imgview=(ImageView)findViewById(R.id.imageView1);
imgview.setVisibility(View.INVISIBLE);  //for invisibility.

imgview.setVisibility(View.VISIBLE);  //make your image visible where ever you want