【问题标题】:How to implement interface into application class?如何在应用程序类中实现接口?
【发布时间】:2017-02-18 14:41:42
【问题描述】:

我有这个应用程序:

public class App extends Application {

    private AppInterface appInterface;

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

        appInterface = (AppInterface) this;
    }

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

使用这个界面:

public interface AppInterface {
    void result();
}

我正在尝试在应用程序中调用一些方法并从界面获取结果:

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

但我收到此错误:

E/AndroidRuntime: 致命异常: main 进程: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 在 android.app.ActivityThread.handleBindApplication(ActivityThread.java:5406) 在 android.app.ActivityThread.-wrap2(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 原因:java.lang.ClassCastException: com.msd.test.App 无法转换为 com.msd.test.AppInterface

我做错了什么?

【问题讨论】:

  • 首先,在Application 中调用getApplicationContext() 是没有意义的。只需使用this。其次,App 包含一个AppInterface 实例。 App 本身不是 AppInterface 实例。
  • 好的,用“this”替换getApplicationContext()很清楚,但是如何解决呢?可以给我看看吗?

标签: android interface


【解决方案1】:

尝试如下界面:

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

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

【讨论】:

  • 非常感谢,现在我明白我的错误了:)
  • 嘿@Michalsx 你应该标记这个答案是正确的,所以如果他们也得到这个问题,其他人也可以得到他们的答案..
  • 对所有人来说,我的答案都是正确的,你应该看到了:)
猜你喜欢
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
相关资源
最近更新 更多