Reputation: 289
I have a code for inflate layout:
mainLayout = findViewById(R.id.mainLayout);
addNewMed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewMed.startAnimation(AnimationUtils.loadAnimation(MedicationAdd.this, R.anim.rotate_complete));
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null);
mesurement = v.findViewById(R.id.mesurement);
mainLayout.addView(v);
}
});
And when I would to find my spinner mesurement
from medication_poles
I have a null-error
medication_poles.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/medPole"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Время приема"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Препарат"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Дозировка"/>
<Spinner
android:id="@+id/mesurement"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
How to find object from inflating layout?
Upvotes: 0
Views: 116
Reputation: 2265
Try this :
addNewMed.startAnimation(AnimationUtils.loadAnimation(MedicationAdd.this,R.anim.rotate_complete));
another_view = getLayoutInflater().inflate(R.layout.medication_poles, null);
mesurement = another_view.findViewById(R.id.mesurement);
mainLayout.addView(another_view);
Upvotes: 0
Reputation: 149
Define New View object with different name like 'view' , v is onClick instance parameter and you should use another one .
try this :
addNewMed.startAnimation(AnimationUtils.loadAnimation(MedicationAdd.this, R.anim.rotate_complete));
View view = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null);
mesurement = view.findViewById(R.id.mesurement);
mainLayout.addView(view);
Upvotes: 0
Reputation: 6067
You are using view which was passed to you in onClick method. that v(view) has only that much scope so you can't use that view object out of that method. v(view object) might give you crash because it may not be able to cast to view class from button class.
Try below code.
//define this object for class level access
View newView;
mainLayout = findViewById(R.id.mainLayout);
addNewMed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNewMed.startAnimation(AnimationUtils.loadAnimation(MedicationAdd.this, R.anim.rotate_complete));
newView = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null);
mesurement = newView.findViewById(R.id.mesurement);
mainLayout.addView(newView);
}
});
if(newView != null)
// do findviewbyid here for other views
Upvotes: 1
Reputation: 2479
I think the problem is that you'r not adding inflated view to a container. So replace:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null)
with:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, mainView,false)
Upvotes: 0