【发布时间】: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