按钮没有响应OnClick(Button not responding to OnClick)

我在XML布局中设置了一个按钮,然后在我的Activity中使用onClickListener进行设置。 单击按钮后,没有任何反应。 我已经设置了TAG,所以一旦按下按钮,它就会在LOG中显示一条消息。

任何帮助,将不胜感激!

编辑:每当我删除代码时,我都发现了问题所在

if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { final FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.add(android.R.id.content, new ImageGridFragment(), TAG); ft.commit(); }

everthing完美无缺。 我不确定我将如何解决这个问题,任何帮助都会受到赞赏......

活动

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>

I've set up a button in my XML layout, then set it up with an onClickListener in my Activity. Once I click the button, nothing happens. I have set up a TAG, so once the button has been pressed, it should show a message in the LOG.

Any help would be appreciated!

EDIT: I have found out what the problem is, whenever I remove the code

if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { final FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.add(android.R.id.content, new ImageGridFragment(), TAG); ft.commit(); }

everthing works perfectly. I'm not sure how I'm going to fix this, any help would be appreciated...

Activity

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>

最满意答案

确保导入import android.view.View.OnClickListener;

或者您也可以尝试这样:

B1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "clicked"); } });

Make sure you are importing import android.view.View.OnClickListener;

or you can also try like this:

B1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "clicked"); } });按钮没有响应OnClick(Button not responding to OnClick)

我在XML布局中设置了一个按钮,然后在我的Activity中使用onClickListener进行设置。 单击按钮后,没有任何反应。 我已经设置了TAG,所以一旦按下按钮,它就会在LOG中显示一条消息。

任何帮助,将不胜感激!

编辑:每当我删除代码时,我都发现了问题所在

if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { final FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.add(android.R.id.content, new ImageGridFragment(), TAG); ft.commit(); }

everthing完美无缺。 我不确定我将如何解决这个问题,任何帮助都会受到赞赏......

活动

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>

I've set up a button in my XML layout, then set it up with an onClickListener in my Activity. Once I click the button, nothing happens. I have set up a TAG, so once the button has been pressed, it should show a message in the LOG.

Any help would be appreciated!

EDIT: I have found out what the problem is, whenever I remove the code

if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { final FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); ft.add(android.R.id.content, new ImageGridFragment(), TAG); ft.commit(); }

everthing works perfectly. I'm not sure how I'm going to fix this, any help would be appreciated...

Activity

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>

最满意答案

确保导入import android.view.View.OnClickListener;

或者您也可以尝试这样:

B1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "clicked"); } });

Make sure you are importing import android.view.View.OnClickListener;

or you can also try like this:

B1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d(TAG, "clicked"); } });