gridview setOnItemClickListener无法正常工作

时间:2013-02-07 15:12:22

标签: java android

我正在使用gridview来显示数字, 我想要下一个活动, 当我点击任何数字 但是当我点击n号时没有任何反应,logcat中没有错误,也没有任何强制关闭。

这是代码

public class CActivity extends Activity 
{
  GridView gridView; 

   public void onCreate(Bundle savedInstanceState) 
            {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cactivity);

             gridView.setOnItemClickListener(new OnItemClickListener()
            {
              public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            Intent myIntent = new Intent(getApplicationContext(), Abc.class);
            startActivity(myIntent);
            //finish();
        }
    });
}
       private void initComponent() 
       {
         gridView = (GridView) findViewById(R.id.month_gridView);
         }

我不知道问题是什么,但gridView.setAdapter(adapter);正在发挥作用。请帮忙

 gridView.setAdapter(adapter);

如果我使用

  GridView gridView = (GridView) findViewById(R.id.month_gridView);

然后app力关闭

logcat的

02-07 21:10:12.721: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-07 21:10:12.780: E/AndroidRuntime(719): FATAL EXCEPTION: main
02-07 21:10:12.780: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.indianic.demo.calendark/com.indianic.demo.calendark.CalendarActivity}: java.lang.NullPointerException
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.os.Looper.loop(Looper.java:123)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-07 21:10:12.780: E/AndroidRuntime(719):  at java.lang.reflect.Method.invokeNative(Native Method)
02-07 21:10:12.780: E/AndroidRuntime(719):  at java.lang.reflect.Method.invoke(Method.java:507)
02-07 21:10:12.780: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-07 21:10:12.780: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-07 21:10:12.780: E/AndroidRuntime(719):  at dalvik.system.NativeStart.main(Native Method)
02-07 21:10:12.780: E/AndroidRuntime(719): Caused by: java.lang.NullPointerException
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.Activity.findViewById(Activity.java:1647)
02-07 21:10:12.780: E/AndroidRuntime(719):  at com.indianic.demo.calendark.CalendarActivity.<init>(CalendarActivity.java:44)
02-07 21:10:12.780: E/AndroidRuntime(719):  at java.lang.Class.newInstanceImpl(Native Method)
02-07 21:10:12.780: E/AndroidRuntime(719):  at java.lang.Class.newInstance(Class.java:1409)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-07 21:10:12.780: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-07 21:10:12.780: E/AndroidRuntime(719):  ... 11 more
02-07 21:10:18.061: I/Process(719): Sending signal. PID: 719 SIG: 9

在设置setOnItemClickListener()

之前,我使用了initComponent()方法

强制关闭错误消失,app正常打开,

但是当我点击这些项目时,下一个活动没有打开或没有任何反应。 :(

如你所说,我尝试了不同的案例,仍然没有成功

              public void onItemClick(AdapterView<?> parent, View v3,
                int position, long id) {


            //setContentView(R.layout.abc);
            switch (position) {
            case 0:
                Intent myIntent = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent);
                break;
            case 1:
                Intent myIntent1 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent1);
                break;
            case 2:
                Intent myIntent2 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent2);
                break;
            case 3:
                Intent myIntent3 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent3);
                break;
            case 4:
                Intent myIntent4 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent4);
                break;
            case 5:
                Intent myIntent5 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent5);
                break;
            case 6:
                Intent myIntent6 = new Intent(getApplicationContext(),Abc.class);
                startActivity(myIntent6);
                break;

            default:
                break;
            }
          }

    });

1 个答案:

答案 0 :(得分:2)

在设置initComponent()

之前,您需要调用自定义setOnItemClickListener()方法
public class CActivity extends Activity 
{
  GridView gridView; 

   public void onCreate(Bundle savedInstanceState) 
            {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cactivity);
    initComponent();
             gridView.setOnItemClickListener(new OnItemClickListener()
            {
              public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
                  switch (position) {
                case 0://do the same for your remaining items
                    Intent myIntent = new Intent(getApplicationContext(), Abc.class);
                    startActivity(myIntent);
                    break;

                default:
                    break;
                }
              }
            //finish();
        }
    });
}
       private void initComponent() 
       {
         gridView = (GridView) findViewById(R.id.month_gridView);
         }