GridView上的OnItemClickListener在片段内没有响应

时间:2012-09-28 10:21:07

标签: android android-fragments android-gridview android-adapter

我想在片段内的网格视图中显示的图像上发生onclick事件时获取图像属性。我试图从传递给onItemClick方法的参数中获取值。当我尝试访问任何参数时,应用程序停止工作。

在烤面包机内部,我用v.getId()/ position / id替换文本“test”。然后应用停止。

这是代码。

    GridView gridView = (GridView) view.findViewById(R.id.imageGrid);
    gridView.setAdapter(new ImageAdapter(this));    
    gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         Toast.makeText(getActivity(), "test"  ,Toast.LENGTH_SHORT).show();
        }});

我是否需要提供任何其他详细信息?或者我犯了什么愚蠢的错误?

更新: 我在这里添加我的错误日志。

09-28 16:03:53.327: W/dalvikvm(15316): threadid=1: thread exiting with uncaught exception (group=0x410f6300)
09-28 16:03:53.337: E/AndroidRuntime(15316): FATAL EXCEPTION: main
09-28 16:03:53.337: E/AndroidRuntime(15316): android.content.res.Resources$NotFoundException: String resource ID #0x10
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.content.res.Resources.getText(Resources.java:229)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.Toast.makeText(Toast.java:265)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.example.store.fragments.AllStoresFragment$1.onItemClick(AllStoresFragment.java:111)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Handler.handleCallback(Handler.java:615)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Looper.loop(Looper.java:137)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

如果您将v.getId()/position/id传递给Toast,它会认为它代表来自id的{​​{1}}资源的String,它将找不到抛出那个例外。试试这个:

strings.xml

Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();