-1

我有 3 个显示列表视图的活动,它有一个显示图标的图像和一个显示数据的文本视图。我想制作一个适配器,可以在这些活动中将通用用于我的列表视图。但是当我运行我的应用程序时,logcat 会显示空指针异常。

08-25 22:43:04.090 28942-28942/com.example.xiaoking.dilibus A/art: art/runtime/thread.cc:1113] 抛出新异常'尝试调用虚拟方法'布尔 android.view.View。 isSelected()' 在空对象引用上出现意外挂起异常:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'int android.view.View.getImportantForAccessibility()'

我知道这个错误的原因是我的 convertview 返回 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();
    }
}

这是我的活动:

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

1 回答 1

0

我在这个函数中发现了问题:

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 为空。

于 2016-08-25T16:08:48.730 回答