java - setOnClickListener 从未触发 - Android/Eclipse

标签 java android eclipse events button

我遇到了一个似乎很常见的问题,但到目前为止,这里的解决方案都没有帮助我。

我正在尝试向我的应用程序添加一个简单的按钮,单击该按钮将启动一个新 Activity 。

该按钮在我的应用程序中显示正常,但当我触摸它时没有任何反应。

我的问题是 setOnClickListener() 永远不会被触发 - 当我调试时它只是跳过它并且 LogCat 中不会显示任何日志消息。

这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sarah.tourapplication.MainActivity"
tools:ignore="MergeRootFrame" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

<RelativeLayout
    android:id="@+id/belowlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/cam_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="Take Picture" />
</RelativeLayout>

这是我的 Activity :

public class MainActivity extends FragmentActivity implements OnClickListener  {

Button camButton;

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
        startActivity(locationUpdatesIntent);

        camButton = (Button)findViewById(R.id.cam_button);

        //add button functionality 

        camButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TEST", "Button was clicked.");
                Intent cameraIntent = new Intent(getApplicationContext(), CameraActivity.class);
                startActivity(cameraIntent);
            }
        });



    }

谁能告诉我哪里出错了?我真的很感激任何指点。

我在 Windows 8 上使用 Eclipse,并且我使用的所有内容都是最新的。

提前致谢!

最佳答案

感谢大家的帮助。事实证明,我在错误的 Activity 中调用了点击处理程序。

关于java - setOnClickListener 从未触发 - Android/Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23451210/

相关文章:

java - 为什么我的 Spring @Autowired 字段为空?

java - 如何使用 retrofit2 接收 Json 数组 ("result")

android - 机器人 :ScrollView Jerky and animation list not work

java - 扩展 Eclipse Java 项目向导的最佳方式?

java - 使用 httpPost 从 java 发送时,node.js req.body 为空

java - JVM 可以缓存对象的字段吗?

java - 将线程从 sleep 状态唤醒

android - 绘图引用

android - Eclipse中android xml布局的横向预览

eclipse - Apache tomcat 服务器启动在 eclipse Kepler 中增加超过 30000 毫秒