I have a FloatingActionButton
in my MainActivity
that, when clicked, takes me to a new Activity called newPlant
. I have this declared in my android manifest as such:
<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. in the newPlant
activity I have another FloatingActionButton
as well as a ImageButton
that are supposed to be clickable. 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). Here is my newPlant.Java
File as well as the accompanying Layout File.
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
<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. This was the issue. Thanks to those who pointed this out.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.