我在我的 XML 布局中设置了一个按钮,然后在我的活动中使用 onClickListener 设置它。一旦我点击按钮,什么都不会发生。我已经设置了一个标签,所以一旦按下按钮,它应该在日志中显示一条消息。
任何帮助,将不胜感激!
编辑:每当我删除代码时,我都发现了问题所在
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
}
一切都完美无缺。我不确定我将如何解决这个问题,任何帮助将不胜感激......
活动
public class ImageGridActivity extends FragmentActivity {
private static final String TAG = "TEST";
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.image_grid_fragment);
if (BuildConfig.DEBUG) {
Utils.enableStrictMode();
}
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
}
Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d(TAG, "clicked");
}
});
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView"
style="@style/PhotoGridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="@dimen/image_thumbnail_size"
android:horizontalSpacing="@dimen/image_thumbnail_spacing"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/image_thumbnail_spacing" >
</GridView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>