尝试按标记获取视图时Android崩溃

时间:2017-02-23 15:19:33

标签: java android crash

所以当我尝试通过之前分配给它的标签获取EditText视图时,应用程序崩溃了(我不确定从何处获取错误日志,控制台为空)。
代码:

`public class StartActivity extends AppCompatActivity {
    private ListView listPeopleDisplay;
    private EditText textCurrentName;
    private ArrayAdapter<String> adapter;
    private LinearLayout roleCheckboxes;
    private int peopleSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        listPeopleDisplay = (ListView) findViewById(R.id.listPlayers);
        listPeopleDisplay.setAdapter(adapter);
        textCurrentName = (EditText) findViewById(R.id.editTextPlayers);
        peopleSize = 0;
    }

    public void sendMessage(View view) {
        if (view.getId() == R.id.buttonAddPlayer) {
            adapter.add(textCurrentName.getText().toString());
            textCurrentName.setText("");
            ++peopleSize;
        } else if (view.getId() == R.id.buttonDoneAddingPlayers) {
            String[] config = new String[peopleSize];
            for (int i = 0; i < peopleSize; i++) {
                config[i] = adapter.getItem(i);
            }
            Configuration.setPeople(config);
            setContentView(R.layout.layout_roles);
            roleCheckboxes = (LinearLayout) findViewById(R.id.layoutRoles);
            for (int i = 0; i < Roles.values().length; i++) {
                if (Roles.values()[i] == Roles.MAFIA ||
                        Roles.values()[i] == Roles.YAKUZA ||
                        Roles.values()[i] == Roles.CIVILIAN) {
                    continue;
                }
                CheckBox cb = new CheckBox(this);
                cb.setText(Roles.values()[i].toString());
                cb.setChecked(false);
                cb.setTag(Roles.values()[i]);
                roleCheckboxes.addView(cb);
            }
            EditText mob = new EditText(this);
            mob.setHint("Mafia count");
            mob.setInputType(InputType.TYPE_CLASS_NUMBER);
            mob.setTag(Roles.MAFIA);
            roleCheckboxes.addView(mob);
            mob = new EditText(this);
            mob.setInputType(InputType.TYPE_CLASS_NUMBER);
            mob.setHint("Yakuza count");
            mob.setTag(Roles.YAKUZA);
            roleCheckboxes.addView(mob);
            mob = new EditText(this);
            mob.setInputType(InputType.TYPE_CLASS_NUMBER);
            mob.setHint("Civilian count");
            mob.setTag(Roles.CIVILIAN);
            roleCheckboxes.addView(mob);
        } else {
            int count = 0;
            CheckBox civ = (CheckBox)roleCheckboxes.findViewWithTag(Roles.CIVILIAN);
<!-- the rest is irrelevant-->`

它在最后一行崩溃。

2 个答案:

答案 0 :(得分:0)

roleCheckboxeselse if (view.getId() == R.id.buttonDoneAddingPlayers)语句中设置,并且您正在尝试在第三个else语句中使用它,该语句超出了它的初始化范围。您应该进行初始化:< / p>

setContentView(R.layout.layout_roles);
roleCheckboxes = (LinearLayout) findViewById(R.id.layoutRoles);

和用法

CheckBox civ = (CheckBox)roleCheckboxes.findViewWithTag(Roles.CIVILIAN); 

在相同的范围内(例如,在相同的if语句中)或者您应该在roleCheckboxes方法中初始化onCreate

答案 1 :(得分:0)

正如logcat所示,我试图将CheckBox强制转换为EditText。我好蠢。抱歉你的时间。