0

I have 2 xml files, a main file where I have a listView and the other where I am making some customisations to my listView. The second xml is used on my Custom ArrayAdapter.

In the second xml i have a ImageView which I would like to set to visible at one moment but I am getting the null pointer exception because i am never setting with setCurrentView the second xml file where is my ImageView.

I am trying to do this:

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

Could you please tell me how to resolve that?

Thank you.

Edit:

Here is more code:

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>

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

custom Adapter:

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

And finnaly the resource class for the 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;
 }
}
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

5 Answers5

4

To set visibility view defined in xml, you need to inflate that view in activity, setContentView do it automatically, and as you are not invoking setContentView on second xml, then use:

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

and then find your view from that view:

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

but it will work only for view object, it does not affect original xml.

jeet
  • 29,001
  • 6
  • 52
  • 53
2

Here is the solution for accessing the view from another class. Note : currentActivity is the context of root class

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

Change to :

statusOk.setVisibility(View.INVISIBLE);

and check is image is ready to use findViewById, like you call the method and after do setContentView or if it really is ImageView, etc.

goodm
  • 7,275
  • 6
  • 31
  • 55
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 
AlexB
  • 7,302
  • 12
  • 56
  • 74
anoop
  • 1
0

Use LayoutInflater.getLayoutInflater() to instantiate layout XML file into its corresponding View objects.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186