我遇到了一个似乎很常见的问题,但到目前为止,这里的解决方案都没有帮助我。
我正在尝试向我的应用程序添加一个简单的按钮,单击该按钮将启动一个新 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/