【问题标题】:setOnClickListener never fired - Android/EclipsesetOnClickListener 从未触发 - Android/Eclipse
【发布时间】:2014-06-20 11:59:39
【问题描述】:

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

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

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

我的问题是 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>

这是我的活动:

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,我使用的一切都是最新的。

提前致谢!

【问题讨论】:

  • 为什么在MainActivity的onCreate中调用startActivity?
  • 因为我想在创建主activity的时候启动一个activity。这是不正确的吗?
  • camButton 在 MainActivity 中工作。findViewById 在 MainActivity 中查找视图,而不是 GetLocationUpdates。

标签: java android eclipse events button


【解决方案1】:

感谢所有帮助。原来我在错误的活动中调用了点击处理程序。

【讨论】:

    【解决方案2】:

    尝试删除线条

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

    看起来您在实际到达要添加侦听器的地步之前开始了不同的活动...

    【讨论】:

      【解决方案3】:

      抱歉,我正在使用手机,但请尝试使用 setOnClickListener(this);并覆盖 onClick 方法。在 onClick 方法中使用 if (view == yourview) 来做你想做的事情。 :)。如果它仍然不起作用,请尝试删除代码开头的 startActivity 方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 2014-01-16
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多