Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

android - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...