视图.findViewById(int)'对空对象引用的问题

问题描述 投票:0回答:2
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at com.test.apptest.Home.HomePageActivity.onCreateOptionsMenu(HomePageActivity.java:366)

366行是。

ImageView searchIcon = (ImageView)searchView.findViewById(R.id.search_mag_icon);

很明显,我试图使用一个有空值的对象引用,我试图了解问题出在哪里,在这里我分享部分片段。

HomePageActivity.java

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.home, menu);
    MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
    Drawable drawable = menu.findItem(R.id.action_search).getIcon();
    if (drawable != null) {
        drawable.mutate();
        drawable.setColorFilter(Color.parseColor(sharedObjects.getPrimaryColor()), PorterDuff.Mode.SRC_ATOP);
    }
    final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
    ImageView searchIcon = (ImageView)searchView.findViewById(R.id.search_mag_icon); // <-- Here is the error
    searchIcon.setColorFilter(Color.parseColor(sharedObjects.getPrimaryColor()));

    ImageView searchClose = searchView.findViewById(R.id.search_close_btn);
    searchClose.setColorFilter(Color.parseColor(sharedObjects.getPrimaryColor()));
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    EditText searchEditText = (EditText) searchView.findViewById(R.id.search_src_text);
    searchEditText.setTextColor(Color.BLACK);
    searchEditText.setHintTextColor(Color.parseColor(sharedObjects.getFooterColor()));

   searchEditText.setTextColor(Color.parseColor(sharedObjects.getPrimaryColor()));
    searchEditText.setDrawingCacheBackgroundColor(Color.parseColor(sharedObjects.getPrimaryColor()));
    searchEditText.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(sharedObjects.getPrimaryColor())));
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.e("Submit", query);

            (menu.findItem(R.id.action_search)).collapseActionView();
            Intent intent = new Intent(HomePageActivity.this, SearchActivity.class);
            intent.putExtra(AppConstants.KEYWORD, query);
            startActivity(intent);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.e("TextChange", newText);
            return false;
        }
    });
    return true;
}

更新

这是完整的代码。

activity_homepage.xml: https:/www.codepile.netpileX5yWyp18

主页面活动.java: https:/www.codepile.netpileoJyYQWJK

任何帮助将是非常感激的,我已经调查了整个下午。

java android android-studio androidx
2个回答
1
投票

首先检查,如果 myActionMenuItem.getActionView() 返回null或不返回。

如果 myActionMenuItem.getActionView() 不为空,然后尝试替换您的findViewById的 searchIcon 以此

ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);

0
投票

从堆栈跟踪中可以看出,对象 searchView 的方法上,你正试图调用 findViewById(R.id.search_mag_icon) 是空的。所以,为了解决这个错误。

  • 对使用的对象增加一个null检查,因为Java不是Null安全的。
  • 确定创建实例对象的代码为 SearchView 以及为什么将其设置为 null.

请分享整个代码,以便我们能够对解决方案做出明智的决定。

© www.soinside.com 2019 - 2024. All rights reserved.