为什么在 android 中查看 return null
Why view return null in android
我有 3 个 activity 显示列表视图,它有一个用于显示图标的图像和一个用于显示数据的文本视图。我想制作一个适配器,可以在这些 activity 上为我的列表视图使用常规。但是当我 运行 我的应用程序时,logcat 显示空指针异常。
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
我知道这个错误的原因是我的 convertview return null。但我不知道为什么以及如何修复它?
这是我的适配器:
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();
}
}
这是我的 activity:
Toolbar toolbar;
CustomCityAdapter myAdapter;
ArrayList<City> arrCities = new ArrayList<City>();
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;
}
我发现这个函数有问题:
getObject(city,position,convertView,parent);
和
convertView = LayoutInflater.from(context).inflate(R.layout.custom_cell_list_cities, parent, false);
如果convertView == null,在getObject方法中,convertView会被赋予新的值。
但是convertView只在getObject方法中有值,在getView中,在getObject(city,position,convertView,parent)下面; , convertView 为空。
我有 3 个 activity 显示列表视图,它有一个用于显示图标的图像和一个用于显示数据的文本视图。我想制作一个适配器,可以在这些 activity 上为我的列表视图使用常规。但是当我 运行 我的应用程序时,logcat 显示空指针异常。
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
我知道这个错误的原因是我的 convertview return null。但我不知道为什么以及如何修复它?
这是我的适配器:
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();
}
}
这是我的 activity:
Toolbar toolbar;
CustomCityAdapter myAdapter;
ArrayList<City> arrCities = new ArrayList<City>();
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;
}
我发现这个函数有问题:
getObject(city,position,convertView,parent);
和
convertView = LayoutInflater.from(context).inflate(R.layout.custom_cell_list_cities, parent, false);
如果convertView == null,在getObject方法中,convertView会被赋予新的值。
但是convertView只在getObject方法中有值,在getView中,在getObject(city,position,convertView,parent)下面; , convertView 为空。