Chris
Chris

Reputation: 81

Calling a method from another class is causing app to crash

I decided to try and make my code more object oriented and avoid repetitive code in another class.

Source code for Activities :

     public class EasyMode extends MainActivity {

            GameActivityPVP game = new GameActivityPVP();

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.game_layout_pvp);

                game.initializeButtons();
            }
        }


    public class GameActivityPVP extends MainActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.game_layout_pvp);

            initializeButtons();
        }

        public void initializeButtons() {

            button[0] = (Button) findViewById(R.id.button1);
        }
}

The second the program gets to the line where I try to call a method using game.methodName(); the program crashes. No compiling errors or anything. I am new to programming in general so please take it easy on me and I tried to simplify my code as much as possible.

Android Monitor/logcat :

W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

and

W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

Upvotes: 0

Views: 1608

Answers (3)

Bhoomika Patel
Bhoomika Patel

Reputation: 1925

You can use another class's method by creating object of parent class.

See below example;

Here you want to use method from 'GameActivityPVP' class. So you need to create one object in this class only.

  public class GameActivityPVP extends MainActivity {

        public static GameActivityPVP mGameActivity;

        public GameActivityPVP getInstance(){
             return mGameActivity; // assign value in onCreate() method.
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.game_layout_pvp);

                mGameActivity = this; // Do not forget this, otherwise you'll get Exception here.
                initializeButtons();
            }

            public void initializeButtons() {

                button[0] = (Button) findViewById(R.id.button1);
            }
    }

Now use this Object in another class 'EasyMode' like this;

if(GameActivityPVP.getInstance()!=null){
    GameActivityPVP.getInstance().initializeButtons();
}

Upvotes: 2

Iulian Popescu
Iulian Popescu

Reputation: 2643

As already stated, you shouldn't use nested activities, they are not supposed to interact like this. If you want two activities to interact you have to do it through an intent. Regarding the duplicated code, you have few solution presented but my personal opinion is that the OOP rules are not followed. If I had to write that logic, I would create a BaseActivity to hold the common logic of the other two activities and use inheritance to extend them.

public class BaseActivity extends Activity {
    protected List<Button> buttons = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_layout_pvp);

        initializeButtons();
    }

    protected void initializeButtons() {

        buttons.add((Button) findViewById(R.id.button1));
    }
}

public class EasyMode extends BaseActivity {
    // Add here logic that is used only in EasyMode activity
}

public class GameActivityPVP extends BaseActivity {
    // Add here logic that is used only in GameActivityPVP activity
}

Note that in this way you don't have to override onCreate again to initialise the buttons and so on. Also, I saw that you used the same layout for both activities, but if you want to use different layouts you can do it as usual and then call initializeButtons.

Upvotes: 0

Abhishek Charismatic
Abhishek Charismatic

Reputation: 366

Try This:

Make one Class Utils:

In Utils:

public class Utils{
private Activity context;
Button button;
public Utils(Activity context) {
    this.context=context;

}

public void inititializeButton(Activity context){
    button[0]= (Button) context.findViewById(R.id.button_flasher);
}

}

And in your Class use:

 public class EasyMode extends MainActivity {

        Utils utils;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.game_layout_pvp);
            utils=new Utils(this);
            utils.initializeButtons();
        }
    }

Upvotes: 0

Related Questions