FindViewById из LayoutInflater - PullRequest
0 голосов
/ 05 декабря 2018

У меня есть код для разметки раздувания:

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);
    }
});

И когда я хотел бы найти свой счетчик mesurement из medication_poles, у меня есть нулевая ошибка

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>

Как найти объект из раздуваемого макета?

Ответы [ 4 ]

0 голосов
/ 05 декабря 2018

Определить новый объект View с другим именем, например, '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);
0 голосов
/ 05 декабря 2018

Попробуйте это:

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);
0 голосов
/ 05 декабря 2018

Вы используете представление, которое было передано вам в методе onClick.этот v (view) имеет только такую ​​большую область видимости, поэтому вы не можете использовать этот объект представления из этого метода.v (просмотр объекта) может привести к сбою, потому что он не может быть приведен к просмотру класса из класса кнопок.

Попробуйте код ниже.

//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
0 голосов
/ 05 декабря 2018

Я думаю, проблема в том, что вы не добавляете раздутый вид в контейнер.Поэтому замените:

 v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, null)

на:

 v = LayoutInflater.from(MedicationAdd.this).inflate(R.layout.medication_poles, mainView,false)
...