开发者社区> 问答> 正文

单击列表视图中的项目时片段崩溃

当我触摸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

展开
收起
montos 2020-03-23 20:40:28 534 0
1 条回答
写回答
取消 提交回答
  • 更换:

    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

    2020-03-23 20:41:17
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载