当我触摸listView中的项目时,应用崩溃
这是logcat
2020-03-15 20:26:50.123 19174-19174 / com.zeroXmohamed.TN19 E / Minikin:无法获取cmap表大小!2020-03-15 20:26:50.158 19174-19202 / com.zeroXmohamed.TN19 E / MemoryLeakMonitorManager:MemoryLeakMonitor.jar不存在!2020-03-15 20:26:54.323 19174-19174 / com.zeroXmohamed.TN19 E / AndroidRuntime:致命例外:主进程:com.zeroXmohamed.TN19,PID:19174 android.content.res.Resources $ NotFoundException:字符串资源编号为#0x3的android.content.res.Resources.getText(Resources.java:410)的android.content.res.HwResources.getText(HwResources.java:465)的android.widget.Toast.makeText(Toast.java: 328),位于com.zeroXmohamed.TN19.fragment.ChercheFragment $ 1.onItemClick(ChercheFragment.java:48),位于android.widget.AdapterView.performItemClick(AdapterView.java:
这是代码
package ...
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.zeroXmohamed.TN19.R;
public class ChercheFragment extends Fragment {
String countryList[] = {"India", "China", "australia", "Portugle", "America", "NewZealand"};
private ListView simpleList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cherche, container,
false);
simpleList= rootView.findViewById(R.id.ListC);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, countryList);
simpleList.setAdapter(adapter);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(getContext(), position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
问题来源:Stack Overflow
更换:
Toast.makeText(getContext(), position, Toast.LENGTH_SHORT).show()
与:
Toast.makeText(getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show()
的版本makeText()以a int作为第二个参数,期望它int是字符串资源ID。你的不是。因此,您需要转换position为String自己。
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。