我不确定为什么按钮不起作用。 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:layout_alignmentParentBottom并且它有效:
<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
答案 0 :(得分:1)
按钮onClickListener中似乎缺少@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);
}
});
如果上述建议不起作用,您可能还想尝试从按钮XML代码中删除android:clickable="true"
。有时它会出现意外行为,并将点击移动到另一个视图(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" />
答案 1 :(得分:1)
您的gridview正在阻止该按钮。而不是:android:layout_below =“@ + id / main_linear”,将其更改为android:layout_above =“@ + id / main_linear”