1

I have this application:

public class App extends Application {

    private AppInterface appInterface;

    @Override
    public void onCreate() {
        super.onCreate();

        appInterface = (AppInterface) this;
    }

    public void something() {
        appInterface.result();
    }
}

Using this interface:

public interface AppInterface {
    void result();
}

And I am trying to call some method inside app and get result from interface:

public class MainActivity extends AppCompatActivity implements AppInterface {

    @Override
    public void result() {
    }

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

        final App app = (App) getApplication();
        app.something();
    }
}

But I am getting this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.something, PID: 13803 java.lang.RuntimeException: Unable to create application com.something.App: java.lang.ClassCastException: com.something.App cannot be cast to com.something.AppInterface at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5406) at android.app.ActivityThread.-wrap2(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.ClassCastException: com.msd.test.App cannot be cast to com.msd.test.AppInterface

What I am doing wrong?

2
  • First, calling getApplicationContext() inside an Application is pointless. Just use this. Second, App contains an AppInterface instance. App itself is not an AppInterface instance. Commented Feb 18, 2017 at 14:45
  • Ok, replace getApplicationContext() by "this" is clear, but how to fix it? Can you show me? Commented Feb 18, 2017 at 14:52

1 Answer 1

6

Try Interface as following :

In App.java :

    public class App extends Application {

        private AppInterface appInterface;

        @Override
        public void onCreate() {
            super.onCreate();
        }

        public void something(AppInterface appInterface) {
            this.appInterface = appInterface;


//save this for future use, and whenver you want to pass data , use this method like-
        appInterface.result();
    }
}

In MainActivity.java

public class MainActivity extends AppCompatActivity implements AppInterface {

    @Override
    public void result() {
      Log.d("result", "called");
    }

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

        final App app = (App) getApplication();
        // pass you reference from here
        app.something(MainActivity.this);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, now I see my mistake :)
Hey @Michalsx you should mark this answer right, So other can also get their answer if they also get this issue..
To all, I marked answer as right, you should see that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.