代码之家  ›  专栏  ›  技术社区  ›  user1832478

未检测到onClickListener

  •  0
  • user1832478  · 技术社区  · 10 年前

    我不知道为什么按钮不起作用。Eclipse没有显示任何红线,我运行应用程序时也没有收到任何错误。我只是觉得应用程序完全忽略了点击。我在XML中使用嵌套布局,这可能是原因吗?

    当我单击这些图片时,它们似乎工作正常,但对于按钮却不起作用。

    代码:

        public class HomeActivity extends Activity{
    
            private Button bSite;
    
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.homeactivity);
    
                //addListenerSiteButton();
                bSite = (Button) this.findViewById(R.id.s_button);
                bSite.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://irishfairy.com/"));
                        startActivity(browserIntent);
                     } 
                  });
    
                GridView gv = (GridView) findViewById(R.id.grid_view);
    
                gv.setAdapter(new ImageAdapter(this));
    
                /**
                 * On Click event for Single Gridview Item
                 * */
               gv.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
    
                        // Sending image id to FullScreenActivity
                        Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                        // passing array index
                        i.putExtra("id", position);
                        startActivity(i);
                    }
                });
            }
    }
    

    XML文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000">
    
        <LinearLayout
            android:id="@+id/main_linear"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/s_button"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/roundedsitebutton"
                android:gravity="center"
                android:padding="15dip"
                android:clickable="true"
                android:text="@string/site" />
    
        </LinearLayout>
    
        <GridView
            android:id="@+id/grid_view"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit"
            android:columnWidth="90dp"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:gravity="center"
            android:stretchMode="columnWidth"
            android:layout_below="@+id/main_linear">
        </GridView>
    
    </RelativeLayout>
    

    编辑:我在XML中交换了android:layout_alignmentParentTop和android:ayout_aligmentParentBottom,它起了作用:

    <LinearLayout
           android:layout_alignParentTop="true"
      </LinearLayout>
    <GridView
         android:layout_alignParentBottom="true"
    </GridView>
    

    不过我希望按钮在底部。

    我运行应用程序时出现此消息:

    03-21 16:51:02.051: D/AbsListView(9953): unregisterIRListener() is called 
    

    编辑:我试着改变:

    android:layout_below="@+id/main_linear"
    

    android:layout_above="@+id/main_linear"
    

    但是,我收到以下错误:

    03-24 08:59:22.221: E/AndroidRuntime(31598): FATAL EXCEPTION: main
    03-24 08:59:22.221: E/AndroidRuntime(31598): Process: com.apps.mobileapp, PID: 31598
    03-24 08:59:22.221: E/AndroidRuntime(31598): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.mobileapp/com.apps.mobileapp.HomeActivity}: java.lang.ClassCastException: android.widget.GridView cannot be cast to android.widget.Button
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   Whitney    10 年前

    你的网格视图挡住了按钮。将其更改为android:layout_below=“@+id/main_linear”,而不是:android:layout_above=“@+id/main-linear”

        2
  •  1
  •   Community Dai    7 年前

    ClickListener上的按钮中似乎缺少@Override注释。尝试将其更改为:

    bSite.setOnClickListener(new OnClickListener() {
    
        @Override //added this line
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://theirishfairydoorcompany.com/"));
            startActivity(browserIntent);
        }
    
    });
    

    您可能还想尝试删除 android:clickable="true" 如果上述建议不起作用,请从按钮XML代码中删除。有时它会出现意外行为,并将单击移动到另一个视图( see this question )

    <Button
            android:id="@+id/s_button"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/roundedsitebutton"
            android:gravity="center"
            android:padding="15dip"
            android:text="@string/site" />