Saturday 24 December 2016

listview - Why view return null in android




I have 3 activity that display listview and it has an image to display icon and textview to display data. I want to make an adapter that can use general for my listview on these activity. But when I run my app, the logcat show me null pointer exception.




08-25 22:43:04.090 28942-28942/com.example.xiaoking.dilibus A/art: art/runtime/thread.cc:1113] Throwing new exception 'Attempt to invoke virtual method 'boolean android.view.View.isSelected()' on a null object reference' with unexpected pending exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference




I know reason for this error is my convertview return null. But I don't know why and how I can fixed it?.




Here is my adapter:



List listObjects;
Context context;
ViewHolder viewHolder;
int typeOfObject;

public CustomCityAdapter(Context context, List objects) {
this.listObjects = objects;
this.context = context;

}

public CustomCityAdapter(Context context, List objects, int typeOfObject) {
this.listObjects = objects;
this.context = context;
this.typeOfObject = typeOfObject;
}

public void setArrayListCity(List listObjects) {
this.listObjects = listObjects;

}

@Override
public int getCount() {
return listObjects.size();
}

@Override
public Object getItem(int position) {
return listObjects.get(position);

}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
City city = new City();

getObject(city,position,convertView,parent);
viewHolder.textView.setText(city.getCityName());
return convertView;
}

private void getObject(Object object, int position, View convertView,ViewGroup parent){
object = getItem(position);
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.custom_cell_list_cities, parent, false);
viewHolder = new ViewHolder();

viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_cell_item_city);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
}


Here is my activity:




Toolbar toolbar;
CustomCityAdapter myAdapter;
ArrayList arrCities = new ArrayList();
CustomListView myListView;
CoordinatorLayout viewCoordinatorLayout;
boolean isDownloadSuccess = true;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choosing_city);
findViewByID();
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
loadCityToListView();
//downloadAndInsertCityToDB();
myListView.setCities(arrCities);
myListView.myListViewSetOnClickListener(this, SearchingTicketActivity.RESULT_CODE_SAVE);
Log.d("Test","OncreateChoosing");

}

@Override
protected void onResume() {
super.onResume();
loadCityToListView();
}

@Override
public void findViewByID() {

toolbar = (Toolbar) findViewById(R.id.choosing_city_activity_toolbar);
myListView = (CustomListView)findViewById(R.id.lv_cities);
viewCoordinatorLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayoutChoosing);
}

@Override
public void setOnClickListener() {
super.setOnClickListener();
}


private void loadCityToListView() {
arrCities = DiLiBusDatabaseHelper.getInstance(this).getListCities();
// CustomListView myListView = new CustomListView(this);
myAdapter = new CustomCityAdapter(this, arrCities);
myListView.setAdapter(myAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_choosing_city, menu);

SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(this);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}


@Override
public boolean onQueryTextSubmit(String query) {
return true;
}

@Override
public boolean onQueryTextChange(String newText) {

return setOnQueryTextListenerSearchView(newText);

}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
protected boolean setOnQueryTextListenerSearchView(String query) {
if (query.equals("")) {

arrCities = DiLiBusDatabaseHelper.getInstance(this).getListCities();
myAdapter.setArrayListCity(arrCities);
myAdapter.notifyDataSetChanged();
myListView.setCities(arrCities);
return true;
}
arrCities = DiLiBusDatabaseHelper.getInstance(this).getListCityByName(query);
myAdapter.setArrayListCity(arrCities);
myAdapter.notifyDataSetChanged();
myListView.setCities(arrCities);

Log.d("List Size: ", arrCities.size() + "");
return true;
}

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}


Answer



I found problem in this function:



getObject(city,position,convertView,parent);


and



convertView = LayoutInflater.from(context).inflate(R.layout.custom_cell_list_cities, parent, false);



If convertView == null, in getObject method, convertView will be assigned new value.



But convertView is only has value in getObject method, and in getView, below getObject(city,position,convertView,parent); , convertView is null.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...