【发布时间】:2013-09-16 12:58:31
【问题描述】:
我在我的 XML 布局中设置了一个按钮,然后在我的 Activity 中设置了一个 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>
【问题讨论】:
-
在您的
onCreate() (like Log.d(TAG, "log cat is showing data");)中尝试另一个调试以检查是否显示日志。如果没有,请尝试重新启动 Eclipse。还要验证您正在导入import android.view.View.OnClickListener; -
可能是 logcat 问题。尝试在 Toast 消息中显示
-
检查您的导入语句以确保您导入
android.view.View.OnClickListener而不是android.content.DialogInterface.OnClickListener -
检查导入包使用view.onclicklistner
-
我已经完成了所有操作,但由于某种奇怪的原因仍然没有任何显示,也许这与我的其他代码有关?