如何在应用程序类中实现接口?

时间:2017-02-18 14:41:43

标签: android interface

我有这个申请:

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

我正在尝试在app中调用一些方法并从接口获取结果:

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:致命异常:主要                                                               过程:com.something,PID:13803                                                               java.lang.RuntimeException:无法创建应用程序com.something.App:java.lang.ClassCastException:com.something.App无法强制转换为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)                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                   在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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试接口如下:

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