从另一个活动的片段调用活动方法

时间:2018-04-06 09:41:25

标签: android android-studio android-fragments android-activity

是否可以从另一个活动的片段调用活动方法?

我在片段

中使用了以下代码
 ((FirstActivity)getActivity()).DisplayMthod();

但它崩溃了我的应用程序。这是我的日志

04-06 15:10:20.934 13443-13443/com.selfie.life.viewpagerwithactivtyslider D/AndroidRuntime: Shutting down VM
04-06 15:10:20.936 13443-13443/com.selfie.life.viewpagerwithactivtyslider E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: com.selfie.life.viewpagerwithactivtyslider, PID: 13443
                                                                                            java.lang.ClassCastException: com.selfie.life.viewpagerwithactivtyslider.MainActivity cannot be cast to com.selfie.life.viewpagerwithactivtyslider.IncludeFile.FirstActivity
                                                                                                at com.selfie.life.viewpagerwithactivtyslider.NotificationFollowingFragment$1.onClick(NotificationFollowingFragment.java:47)
                                                                                                at android.view.View.performClick(View.java:5269)
                                                                                                at android.view.View$PerformClick.run(View.java:21556)
                                                                                                at android.os.Handler.handleCallback(Handler.java:815)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                                at android.os.Looper.loop(Looper.java:207)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
04-06 15:10:27.399 13443-13452/com.selfie.life.viewpagerwithactivtyslider I/System: FinalizerDaemon: finalize objects = 102

0 个答案:

没有答案