I am trying to make a popup menu appear when I click one of the items on the ListView, but I cant get the position of the item.
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ItemList item= adapter.getItem(position);
Toast.makeText(getContext(), item.getTxtNomeItem().toString(), Toast.LENGTH_SHORT).show();
//showPopup(adapterView);
}
});
That code gives me "Attempt to invoke virtual method 'java.lang.Object com.example.securityaplication.ItemArrayAdapter.getItem(int)' on a null object reference" error.
I have made the same thing in another project and it worked fine:
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
modelo item = adapter.getItem(position);
Toast.makeText(getApplicationContext(),
item.getNome().toString (), Toast.LENGTH_SHORT).show();
}
});
The only difference is that I used "getContext" instead of "getApplicationContext()" because it didnt accept "getApplicationContext()" and that the first one is in a fragment and the latter one is in a activity.
My Fragment where it gives me the error:
public class HomeFragment extends Fragment implements PopupMenu.OnMenuItemClickListener {
ItemArrayAdapter adapter;
private ListView listax;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
listax = (ListView)view.findViewById(R.id.listViewx);
ArrayList<ItemList> ItemList = new ArrayList<>();
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
listax.setAdapter(itemArrayAdapter);
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ItemList item= adapter.getItem(position);
Toast.makeText(getContext(), item.getTxtNomeItem().toString(), Toast.LENGTH_SHORT).show();
//showPopup(adapterView);
}
});
return view;
}
public void showPopup(View v){
PopupMenu popup = new PopupMenu(getActivity(), v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menupopup);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch(menuItem.getItemId()){
case R.id.itemAlterar:
Toast.makeText(getActivity(), "Item 1 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemExcluir:
Toast.makeText(getActivity(), "Item 2 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemEmprestar:
Toast.makeText(getActivity(), "Item 3 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemRecuperar:
Toast.makeText(getActivity(), "Item 3 clicked", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}}
}
Don't forget to use the class member variable "adapter" when you setup the ListAdapter.
//ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
//listax.setAdapter(itemArrayAdapter);
adapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
listax.setAdapter(adapter);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.