获得发生空指针异常的错误并尝试调用虚方法
public class after10th extends AppCompatActivity {
ArrayAdapter<String> adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after10th);
ListView lv = (ListView) findViewById(R.id.searchten);
ArrayList<String> after_ten = new ArrayList<>();
after_ten.addAll(Arrays.asList(getResources().getStringArray(R.array.after_ten)));
adapter = new ArrayAdapter<>(after10th.this, android.R.layout.simple_list_item_1, after_ten);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.menusearchten);
SearchView searchView = (SearchView) item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);`enter code here`
}
}
这是错误:
09-23 19:09:17.242 9000-9000 / com.example.supriya.career_guidance E / AndroidRuntime:FATAL EXCEPTION:main
处理:com.example.supriya.career_guidance,PID:9000
java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView $ OnQueryTextListener)&#39;在空对象引用上
答案 0 :(得分:0)
错误消息几乎完全表示item.getActionView()
在此行返回null:
SearchView searchView = (SearchView) item.getActionView();