我遇到的问题似乎很常见,但到目前为止,这里的解决方案都没有帮助过我。
我正在尝试向我的应用添加一个简单的按钮,该按钮会在点击时启动新活动。
按钮在我的应用程序中显示正常,但是当我触摸它时没有任何反应。
我的问题是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,我使用的所有内容都是最新的。
提前致谢!
答案 0 :(得分:1)
感谢所有帮助。事实证明,我在错误的活动中调用了点击处理程序。
答案 1 :(得分:0)
尝试删除行
Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
startActivity(locationUpdatesIntent);
看起来你开始尝试不同的活动才真正达到了你添加听众的程度......
答案 2 :(得分:0)
抱歉,我在手机上,但尝试使用setOnClickListener(this);并覆盖onClick方法。在onClick方法中使用if(view == yourview)来做你想要的w / e。 :)。如果仍然无效,请尝试在代码的最开头删除startActivity方法