无法从列表视图中获取项目位置:尝试在空对象引用上调用虚拟方法...
Posted
技术标签:
【中文标题】无法从列表视图中获取项目位置:尝试在空对象引用上调用虚拟方法...【英文标题】:Can't get item position from listview: Attempt to invoke virtual method ... on a null object reference 【发布时间】:2021-07-24 06:48:01 【问题描述】:当我单击 ListView 上的一项时,我试图让一个弹出菜单出现,但我无法获得该项的位置。
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);
);
该代码给了我“尝试在空对象引用上调用虚拟方法'java.lang.Object com.example.securityaplication.ItemArrayAdapter.getItem(int)'”错误。
我在另一个项目中做了同样的事情,效果很好:
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();
);
唯一的区别是我使用“getContext”而不是“getApplicationContext()”,因为它不接受“getApplicationContext()”,并且第一个在片段中,而后一个在活动中。
我的 Fragment 给了我错误:
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;
【问题讨论】:
【参考方案1】:在设置 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);
【讨论】:
以上是关于无法从列表视图中获取项目位置:尝试在空对象引用上调用虚拟方法...的主要内容,如果未能解决你的问题,请参考以下文章
自定义视图上的数据绑定“无法在空引用对象上引用 .setTag”
显示来自sqlite数据库的数据时“尝试在空对象引用上调用虚拟方法”[重复]
尝试在空对象引用上调用虚拟方法“java.lang.String android.content.Context.getPackageName()”
错误 - 尝试在空对象引用上调用虚拟方法“int android.graphics.Bitmap.getHeight()”