8

我使用 gridview 来显示数字,所以如果我点击任何数字,下一个活动应该开始。我尝试了这段代码,但应用程序崩溃了

         private GridView gridView = null;

      gridView.setOnClickListener(new OnClickListener()

    {
              public void onClick(View v5) 

        {
            setContentView(R.layout.Abc);
            Intent myIntent = new 

 Intent(getApplicationContext(),Abc.class);
            startActivity(myIntent);

        }
    });

这是gridview的xml代码

     <GridView
    android:id="@+id/month_gridView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/calendar_days"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="8dp"
    android:background="@color/grid_background"
    android:fadingEdge="none"
    android:gravity="top|left"
    android:horizontalSpacing="2dp"
    android:listSelector="@android:color/transparent"
    android:numColumns="7"
    android:padding="1dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="2dp">
</GridView>

logcat 文件日志

 02-04 00:10:50.603: D/AndroidRuntime(341): Shutting down VM
    02-04 00:10:50.603: W/dalvikvm(341): threadid=1: thread exiting with uncaughtexception(group=0x40015560)
    02-04 00:10:50.635: E/AndroidRuntime(341): FATAL EXCEPTION: main
    02-04 00:10:50.635: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indianic.demo.calendark/com.indianic.demo.calendark.CalendarActivity}: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:507)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method)
    02-04 00:10:50.635: E/AndroidRuntime(341): Caused by: java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.widget.AdapterView.setOnClickListener(AdapterView.java:750)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at com.indianic.demo.calendark.CalendarActivity.onCreate(CalendarActivity.java:126)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-04 00:10:50.635: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    02-04 00:10:50.635: E/AndroidRuntime(341):  ... 11 more
    02-04 00:10:53.203: I/Process(341): Sending signal. PID: 341 SIG: 9
4

3 回答 3

42

GridView 就像一个 ListView

你应该使用这样的东西

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

            // DO something

        }
    });

代码并不完美

参考见http://developer.android.com/reference/android/widget/GridView.html

于 2013-02-03T18:28:22.060 回答
4

您应该使用 onItemClickListener 而不是 onClickListener。

于 2013-02-03T18:27:29.103 回答
1

gridView.onClickListener() 将作为视图作为网格的侦听器。gridView.setOnItemClickListener() 将是网格中项目的侦听器。它将位置作为参数,指示您正在单击的项目。parent 参数将指示 gridView 本身。您可以使用它来确定项目的尺寸。就像是

view.setMinHeight(parent.getHeight()/n);
于 2013-02-04T07:34:45.597 回答