活动中的findViewById()对于某些片段中的元素返回null,但对其他片段则不返回null

时间:2019-02-02 17:12:26

标签: java android

首先,我了解到findViewById返回空值的问题很多,我已经阅读了许多问题,但是还没有一个问题解决了我的问题。 名为match的活动使用TabLayout显示三个不同的片段。 dataify()从每个片段中获取元素,并将其放入DeepSpace对象中。只有fragment_capabilities中的元素返回null,我无法弄清原因。我如何让他们返回正确的元素?这些片段的Java文件本质上是相同的。

public class match extends AppCompatActivity implements scoring.OnFragmentInteractionListener,
    capabilities.OnFragmentInteractionListener, Endgame.OnFragmentInteractionListener{
  ...
  public DeepSpace dataify(){
    // from activity_main:
    DeepSpace deepSpace = new DeepSpace();
    deepSpace.setTeamNum(MainActivity.teamNum);
    deepSpace.setMatchNum(MainActivity.matchNum);
    deepSpace.setAlliance(MainActivity.allianceColor == "red" ? 0 : 1); // 0 = red; 1 = blue
    // from fragment_capabilities:
    RadioGroup rdogCargo = (RadioGroup) findViewById(R.id.rdogCargo); //returns null
    RadioGroup rdogHatch = (RadioGroup) findViewById(R.id.rdogHatch); //returns null
    // from fragment_scoring:
    RadioGroup rdogCycleOne = (RadioGroup) findViewById(R.id.rdogL1);
    RadioGroup rdogCycleHigh = (RadioGroup) findViewById(R.id.rdogHL);
    // from fragment_end:
    RadioGroup rdogSelf = (RadioGroup) findViewById(R.id.rdogSelf);
    RadioGroup rdogAssist = (RadioGroup) findViewById(R.id.rdogAssist);
    RadioGroup rdogAsistTwo = (RadioGroup) findViewById(R.id.rdogAssist2);
    CheckBox chkDescend = (CheckBox) findViewById(R.id.chkDescend);
    ...
    // setting variables in deepSpace to values from above elements here
    return deepSpace;
  }
}

编辑R.id.rdogCargo给了我正确的ID,只是findViewById返回了空值

0 个答案:

没有答案