碎片在点击列表视图中的项目时崩溃。

huangapple go评论83阅读模式
英文:

Fragment crash when click on item in listview

问题

当我在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:字符串资源ID#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:321)
在android.widget.AbsListView.performItemClick(AbsListView.java:1217)
在android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)
在android.widget.AbsListView$3.run(AbsListView.java:4151)
在android.os.Handler.handleCallback(Handler.java:808)
在android.os.Handler.dispatchMessage(Handler.java:101)
在android.os.Looper.loop(Looper.java:166)
在android.app.ActivityThread.main(ActivityThread.java:7529)
在java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

以下是代码:

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[] = {"印度", "中国", "澳大利亚", "葡萄牙", "美国", "新西兰"};
    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;
    }
}
英文:

when i touch an item in the listView, the app crash

here's the logcat

> 2020-03-15 20:26:50.123 19174-19174/com.zeroXmohamed.TN19 E/Minikin:
> Could not get cmap table size! 2020-03-15 20:26:50.158
> 19174-19202/com.zeroXmohamed.TN19 E/MemoryLeakMonitorManager:
> MemoryLeakMonitor.jar is not exist! 2020-03-15 20:26:54.323
> 19174-19174/com.zeroXmohamed.TN19 E/AndroidRuntime: FATAL EXCEPTION:
> main
> Process: com.zeroXmohamed.TN19, PID: 19174
> android.content.res.Resources$NotFoundException: String resource ID #0x3
> at android.content.res.Resources.getText(Resources.java:410)
> at android.content.res.HwResources.getText(HwResources.java:465)
> at android.widget.Toast.makeText(Toast.java:328)
> at com.zeroXmohamed.TN19.fragment.ChercheFragment$1.onItemClick(ChercheFragment.java:48)
> at android.widget.AdapterView.performItemClick(AdapterView.java:321)
> at android.widget.AbsListView.performItemClick(AbsListView.java:1217)
> at android.widget.AbsListView$PerformClick.run(AbsListView.java:3203)
> at android.widget.AbsListView$3.run(AbsListView.java:4151)
> at android.os.Handler.handleCallback(Handler.java:808)
> at android.os.Handler.dispatchMessage(Handler.java:101)
> at android.os.Looper.loop(Looper.java:166)
> at android.app.ActivityThread.main(ActivityThread.java:7529)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

here's the code

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[] = {&quot;India&quot;, &quot;China&quot;, &quot;australia&quot;, &quot;Portugle&quot;, &quot;America&quot;, &quot;NewZealand&quot;};
    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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(),
                android.R.layout.simple_list_item_1, countryList);
        simpleList.setAdapter(adapter);


        simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                                    int position, long arg3) {
                Toast.makeText(getContext(), position, Toast.LENGTH_SHORT).show();



            }
        });

        return rootView;
    }


}

答案1

得分: 1

用以下代码替换:

Toast.makeText(getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show()

makeText() 方法的版本,它将第二个参数作为 int,预期该 int 是一个字符串资源 ID。而你的参数不是。所以,你需要自己将 position 转换为一个 String

英文:

Replace:

Toast.makeText(getContext(), position, Toast.LENGTH_SHORT).show()

with:

Toast.makeText(getContext(), Integer.toString(position), Toast.LENGTH_SHORT).show()

The version of makeText() that takes an int as the second parameter is expecting that int to be a string resource ID. Yours is not. So, you need to convert position to a String yourself.

huangapple
  • 本文由 发表于 2020年3月16日 03:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/60696808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定