私はmItemAddFriendsを呼び出すときにnullオブジェクトを取得していると思うので、私の意見では、onBindViewHolder()メソッドと関係がある可能性があるヌルポインタ例外が発生しています。取得(位置);私のコードとクラスは以下の通りです。RecyclerView:onBindViewHolderのヌルオブジェクト参照item.get(位置)
エラーログ:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.pmp_vip.personachat.ItemAddFriends.getUsername()' on a null object reference
at com.pmp_vip.personachat.ItemViewHolder.bind(ItemViewHolder.java:19)
at com.pmp_vip.personachat.RVAdapter$override.onBindViewHolder(RVAdapter.java:31)
...
私のクラス:
RVAdapterクラス:
public class RVAdapter extends RecyclerView.Adapter<ItemViewHolder> { private List<ItemAddFriends> mItemAddFriends; public RVAdapter(List<ItemAddFriends> mItemAddFriends) { this.mItemAddFriends = mItemAddFriends; } public void setmItemAddFriends(List<ItemAddFriends> mItemAddFriends{ this.mItemAddFriends = mItemAddFriends; } @Override public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_friends, parent, false); return new ItemViewHolder(view); } @Override public void onBindViewHolder(ItemViewHolder itemViewHolder, int position) { final ItemAddFriends item = mItemAddFriends.get(position); itemViewHolder.bind(item); } @Override public int getItemCount() { return mItemAddFriends.size(); } public void setFilter(List<ItemAddFriends> itemAddFriends) { mItemAddFriends = new ArrayList<>(); mItemAddFriends.addAll(itemAddFriends); notifyDataSetChanged(); } }
ItemViewHolderクラス:
public class ItemViewHolder extends RecyclerView.ViewHolder { public TextView username_textView; public TextView name_textView; public ItemViewHolder(View view) { super(view); username_textView = (TextView) view.findViewById(R.id.item_username); name_textView = (TextView) view.findViewById(R.id.item_name); } public void bind(ItemAddFriends itemAddFriends) { username_textView.setText(itemAddFriends.getUsername()); name_textView.setText(itemAddFriends.getName()); } }
AddFriendsActivityクラス:
public class AddFriendsActivity extends AppCompatActivity implements SearchView.OnQueryTextListener { private RecyclerView recyclerView; private List<ItemAddFriends> mItemAddFriends; private RVAdapter adapter; private static final String SERVER_ADDRESS = "http://10.0.2.2/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_friends); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mItemAddFriends = new ArrayList<>(); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); adapter = new RVAdapter(mItemAddFriends); recyclerView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.add, menu); final MenuItem item = menu.findItem(R.id.action_add); final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); searchView.setMaxWidth(Integer.MAX_VALUE); searchView.setQueryHint(getResources().getString(R.string.e_username)); searchView.setIconified(false); searchView.setOnQueryTextListener(this); searchView.setOnCloseListener(new SearchView.OnCloseListener() { @Override public boolean onClose() { finish(); return true; } }); MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() { @Override public boolean onMenuItemActionCollapse(MenuItem item) { adapter.setFilter(mItemAddFriends); return true; } @Override public boolean onMenuItemActionExpand(MenuItem item) { return true; } }); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { final List<ItemAddFriends> filteredItemList = filter(mItemAddFriends, newText); adapter.setmItemAddFriends(filteredItemList); adapter.setFilter(filteredItemList); return true; } private List<ItemAddFriends> filter(List<ItemAddFriends> list, String query) { query = query.toLowerCase(); final List<ItemAddFriends> filteredItemList = new ArrayList<>(); SearchUser searchUser = new SearchUser(query); searchUser.execute(); filteredItemList.add(searchUser.getItemAddFriends()); return filteredItemList; } private class SearchUser extends AsyncTask<Void, Void, Void> { String query; ItemAddFriends itemAddFriends; public SearchUser(String query) { this.query = query; } public ItemAddFriends getItemAddFriends() { return itemAddFriends; } @Override protected Void doInBackground(Void... params) { String jsonData = ""; String result = ""; JSONObject object = null; try { OkHttpClient client = new OkHttpClient(); FormBody.Builder builder = new FormBody.Builder() .add("query", query); RequestBody formBody = builder.build(); Request request = new Request.Builder() .url(SERVER_ADDRESS + "search.user.php") .post(formBody) .build(); Response responses = null; try { responses = client.newCall(request).execute(); jsonData = responses.body().string(); } catch (IOException e) { e.printStackTrace(); } try { JSONObject Jobject = new JSONObject(jsonData); JSONArray Jarray = Jobject.getJSONArray("users"); for (int i = 0; i < Jarray.length(); i++) { object = Jarray.getJSONObject(i); result = object.getString("firstname"); } } catch (JSONException e) { e.printStackTrace(); } } catch (Exception e){ e.printStackTrace(); } itemAddFriends = new ItemAddFriends(result, result); return null; } } }
私はすべてがItemViewHolderクラスname_textView.setText(itemAddFriends.getName())内のメソッドバインド()という問題を除いてうまくいくと思います。 Null Pointer Exceptionが発生します。 私はそれを修正する方法がわかりません。
ありがとうございました!