【问题标题】:FindViewById from LayoutInflater来自 LayoutInflater 的 FindViewById
【发布时间】:2018-12-05 09:22:55
【问题描述】:

我有一个膨胀布局的代码:

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>

如何从膨胀布局中找到对象?

【问题讨论】:

  • 发帖res/layout/medication_poles.xml
  • 查看问题,刷新

标签: java android android-inflate


【解决方案1】:

您正在使用在 onClick 方法中传递给您的视图。 v(view) 只有这么大的范围,因此您不能在该方法之外使用该视图对象。 v(view object) 可能会让你崩溃,因为它可能无法从按钮类转换为视图类。

试试下面的代码。

//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

【讨论】:

    【解决方案2】:

    我认为问题在于您没有向容器添加膨胀视图。 所以替换:

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

    与:

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

    【讨论】:

      【解决方案3】:

      用不同的名称定义新的视图对象,例如 '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);
      

      【讨论】:

        【解决方案4】:

        试试这个:

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

        【讨论】:

          猜你喜欢
          • 2013-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-22
          • 1970-01-01
          • 2014-05-19
          相关资源
          最近更新 更多