[英]Android OnClickListener Not Being Called
I have a FloatingActionButton
in my MainActivity
that, when clicked, takes me to a new Activity called newPlant
. 我的
MainActivity
中有一个FloatingActionButton
,单击该按钮newPlant
我带到一个名为newPlant
的新Activity。 I have this declared in my android manifest as such: 我在我的android清单中声明了这样的内容:
<activity
android:name="newPlant"
android:label="@string/app_name">
</activity>
The Label is the same as MainActivity
because I didn't really know what the label was for. 标签与
MainActivity
相同,因为我并不真正知道标签的用途。 in the newPlant
activity I have another FloatingActionButton
as well as a ImageButton
that are supposed to be clickable. 在
newPlant
活动中,我还有另一个FloatingActionButton
和一个ImageButton
,它们应该是可单击的。 When I first began having trouble I found and went through this post , which helped. 当我第一次遇到麻烦时,我发现并浏览了这篇文章 ,这很有帮助。 Now the problem is the none of the code inside the
onClickListeners
is being executed (I have Log.D methods in each listener that are not being called). 现在的问题是代码的无内部
onClickListeners
正在执行(我Log.D在每一个不被称为监听方法)。 Here is my newPlant.Java
File as well as the accompanying Layout File. 这是我的
newPlant.Java
文件以及随附的布局文件。
public class newPlant extends AppCompatActivity implements OnClickListener {
private ImageButton plantCam;
private FloatingActionButton savePlant;
private EditText newPlantName;
private EditText newPlantWF;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newplant);
plantCam = (ImageButton)findViewById(R.id.newPlantImage);
savePlant = (FloatingActionButton)findViewById(R.id.savePlant);
newPlantName = findViewById(R.id.newPlantName);
newPlantWF = findViewById(R.id.newPlantWF);
setContentView(R.layout.newplant);
plantCam.setOnClickListener(this);
savePlant.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
plantCam.setImageBitmap(bitmap);
}
public void onClick(View v){
switch(v.getId()){
case R.id.newPlantImage:{
Log.d("plantCam", "clicked");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
break;
}case R.id.savePlant:{
Log.d("SavePlant", "clicked");
MyApplication.myPlantList.add(new Plant(newPlantName.getText().toString(), plantCam.getDrawable(), newPlantWF.getText().toString()));
MyApplication.savePlants();
finish();
Log.d("SavePlant", "finished saving plant" + MyApplication.myPlantList.toString());
break;
}
}
}
}
xml XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/newPlantName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<EditText
android:id="@+id/newPlantWF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:ems="10"
android:inputType="numberSigned"
android:text="Days between Watering"/>
<ImageButton
android:id="@+id/newPlantImage"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="57dp"
android:layout_marginStart="54dp"
android:src="@mipmap/add_plant"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/savePlant"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/newPlantImage"
android:layout_gravity="end|bottom"
android:layout_marginEnd="20dp"
android:layout_marginTop="-57dp"
android:scaleType="center"
android:src="@mipmap/add_plant" />
</RelativeLayout>
For some reason I called setContentView()
twice. 由于某种原因,我两次调用了
setContentView()
。 This was the issue. 这就是问题所在。 Thanks to those who pointed this out.
感谢那些指出这一点的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.