我可以创建其他类活动的对象呢? [英] Can i Create the object of a activity in other class?

查看:146
本文介绍了我可以创建其他类活动的对象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义在mainactivity功能现在我想用另一个类我app.I访问已使用该对象我称之为function.It没有显示任何错误,但它不执行创造了mainactivity的对象每次我试图执行应用程序崩溃? 任何解决方案

i have defined a function in mainactivity now i want to access with another class in my app.I have created a object of the mainactivity by using that object i have called the function.It is not showing any error but its not executing every time i try to execute the app crashes? any solutions

推荐答案

活动A应具有可变

static ActivityA activityA;

在的onCreate状态:

In onCreate state:

activityA = this;

和添加这个方法:

public static ActivityA getInstance(){
   return   activityA;
 }

在活动B,通话

ActivityA.getInstance().myFunction(); //call myFunction using activityA

这篇关于我可以创建其他类活动的对象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆