简体   繁体   中英

Android - How to add radio buttons to a radiogroup in another Activity? (programatically)

I'm very new to programming with Android, so I'm making a simple text RPG to practice developing for the platform. The player has access to a menu, which shows their items as a group of Radio Buttons. That way one can be selected to Equip/sell/etc. The game is handled in a completely different Activity. I want a random event that can happen in the Game activity to be able to add a new custom Radio button to the Inventory Activity page. On my Inventory Activity page, I've written a simple method:

    public void addRadioButtons(){

        RadioGroup items=(RadioGroup) findViewById(R.id.invItems);
        RadioButton newItem = new RadioButton(this);
        newItem.setText("New Rare Item");
        newItem.setId(idCounter);
        idCounter++;
        items.addView(newItem);
}

When the random drop event is calculated in the Game Event, it just does a object.addRadioButtons() method call. This force closes every time, no matter what I try. I've also tried pre-formatting the button before adding it, also to no avail. Am I missing something?

It seems, that the reason in idCounter value. In Android view id must be calculated according to certain rules. Look at this post for more detals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM