【问题标题】:Button not responding to OnClick按钮不响应 OnClick
【发布时间】: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
  • 我已经完成了所有操作,但由于某种奇怪的原因仍然没有任何显示,也许这与我的其他代码有关?

标签: android button onclick


【解决方案1】:

确保您正在导入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");

    }
});

【讨论】:

  • 我确定我正在导入“import android.view.View.OnClickListener;”并使用了您提供给我的代码/添加了“查看”。在 OnClickListener 前面,它仍然什么都不做?
【解决方案2】:

以下应该有效:

Button B1 = (Button) findViewById(R.id.button1);
B1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Log.d(TAG, "clicked");

    }
}),

编辑:

它仍然不起作用的原因是你必须导入视图。如果您使用的是 Eclipse,请按 Ctrl+Shift+O 来修复所有导入。

否则添加

 import android.view.View.OnClickListener;

【讨论】:

    【解决方案3】:

    因为您无法在 View 上看到 clickEvent。你的onClickListener 没有被调用。尝试在您的View 上添加onClickListener。就像下图的sn-p:-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2023-03-19
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多