我正在尝试将搜索图标添加到工具栏,按下该图标将打开搜索栏。
但我尝试的每种方法都不起作用,并且没有图标出现。现在我收到此错误,但找不到解决方案:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference
at pl.michalz.hideonscrollexample.activity.partthree.PartThreeActivity.onCreateOptionsMenu(PartThreeActivity.java:62)
Run Code Online (Sandbox Code Playgroud)
这是我的主要活动:
public class PartThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppThemeBlue);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_part_three);
initToolbar();
initViewPagerAndTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem seachItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(seachItem);
searchView.setOnQueryTextListener(
new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
Log.d("myApp", …Run Code Online (Sandbox Code Playgroud) 我正在使用此教程:https : //www.youtube.com/watch?v=I2eYBtLWGzc
https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/
起初我得到了错误:
java.lang.NullPointerException: Attempt to invoke virtual method android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference
at pl.michalz.hideonscrollexample.adapter.parttwo.MyAdapter.onCreateViewHolder(MyAdapter.java:37)
at pl.michalz.hideonscrollexample.adapter.parttwo.MyAdapter.onCreateViewHolder(MyAdapter.java:18)
Run Code Online (Sandbox Code Playgroud)
我从以下方面进行了更改:
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.w("myApp", "onCreateViewHolder ");
View view = layoutInflater.inflate(R.layout.recycler_item, parent, false);
return new ViewHolder(view);
}
Run Code Online (Sandbox Code Playgroud)
至:
View view = layoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
Run Code Online (Sandbox Code Playgroud)
它修复了错误,但仅在几次之后我就厌倦了模拟App。我真的很难完成这项工作,所以我不知道这是正确的解决方法还是我的代码不好,以后可能会导致更多错误。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private LayoutInflater layoutInflater;
private ArrayList<Receptas> receptasL = new ArrayList<>();
public MyAdapter(Context context) {
//layoutInflater = layoutInflater.from(context);
}
public void …Run Code Online (Sandbox Code Playgroud) android android-volley android-recyclerview jsonobjectrequest
我有两个ArrayLists,
ArrayList<Item> array1 = new ArrayList<>();
// and
ArrayList<Item> array2 = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
在Item类中我也有默认值 boolean check = false;
我检查,在清理所有内容之前array2.clear(),需要从array1添加到array2:
array2.clear();
for (Item i : array1) {
if (.....) {
array2.add(i);
}
}
Run Code Online (Sandbox Code Playgroud)
如果在array2 boolean check中更改false为true,则在array1中也会更改它.如何将它们分开,以便下次向array2添加元素时,布尔检查不会复制为true?