为啥在android中查看返回null [重复]

Posted

技术标签:

【中文标题】为啥在android中查看返回null [重复]【英文标题】:Why view return null in android [duplicate]为什么在android中查看返回null [重复] 【发布时间】:2017-01-02 03:38:21 【问题描述】:

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

08-25 22:43:04.090 28942-28942/com.example.xiaoking.dilibus A/art: art/runtime/thread.cc:1113] 抛出新异常'尝试调用虚拟方法'boolean android.view .View.isSelected()' on a null object reference' 出现意外挂起异常:java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on an null object reference

我知道这个错误的原因是我的转换视图返回 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;

【问题讨论】:

请显示您调用 View.isSelected 方法的活动代码。我认为您在调用 isSelected 方法之前缺少定义视图。 @HoàngVũNam 我刚刚编辑了我的问题。我认为活动没有问题。 Bcuz 如果我编辑代码以使列表视图仅加载一个列表对象,它会完美运行。 我刚刚添加了我的答案。让检查:***.com/questions/7893492/… 【参考方案1】:

我发现这个函数有问题:

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

【讨论】:

谢谢你,兄弟,你的问题正是如此,解决方案是让 getObject 返回 View :)。非常感谢。 很高兴帮助你:D

以上是关于为啥在android中查看返回null [重复]的主要内容,如果未能解决你的问题,请参考以下文章

全局变量在android studio中返回null [重复]

为啥 System.getProperty("home.dir") 返回 null [重复]

为啥在检查 null 的长度时我没有收到错误 [重复]

Android - Google地图为位置返回null [重复]

为啥 fusedLocationProviderClient.getLast 位置总是返回 null?

ImageView返回由Android中的Drawable文件设置的Null [重复]