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?
getApplicationContext()inside anApplicationis pointless. Just usethis. Second,Appcontains anAppInterfaceinstance.Appitself is not anAppInterfaceinstance.