从 LayoutInflater FindViewById
FindViewById from LayoutInflater
我有一个用于膨胀布局的代码:
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);
}
});
当我想从 medication_poles
中找到我的微调器 mesurement
时,我遇到了空错误
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>
如何从充气布局中找到对象?
我认为问题在于您没有向容器添加膨胀视图。
所以替换:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null)
与:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, mainView,false)
试试这个:
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);
您正在使用在 onClick 方法中传递给您的视图。 v(view) 只有那么大的范围,所以你不能在那个方法之外使用那个视图对象。
v(view object) 可能会让您崩溃,因为它可能无法从按钮 class.
强制转换为视图 class
试试下面的代码。
//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
用不同的名称定义新视图对象,例如 'view',v 是 onClick 实例参数,您应该使用另一个。
试试这个:
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);
我有一个用于膨胀布局的代码:
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);
}
});
当我想从 medication_poles
中找到我的微调器 mesurement
时,我遇到了空错误
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>
如何从充气布局中找到对象?
我认为问题在于您没有向容器添加膨胀视图。 所以替换:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null)
与:
v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, mainView,false)
试试这个:
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);
您正在使用在 onClick 方法中传递给您的视图。 v(view) 只有那么大的范围,所以你不能在那个方法之外使用那个视图对象。 v(view object) 可能会让您崩溃,因为它可能无法从按钮 class.
强制转换为视图 class试试下面的代码。
//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
用不同的名称定义新视图对象,例如 'view',v 是 onClick 实例参数,您应该使用另一个。
试试这个:
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);