活动中可用的调用方法
我正在尝试从 java 文件调用 Activity
中可用的方法。
通过 java 文件中的这段代码,调用变得很好,控制权转移到函数。
myActivity my = new myActivty();
myActivity.method1();
现在我的问题是我无法给出toast
消息或显示alertdialog
,当我使用Toast
消息时它给出空指针异常,
07-20 15:13:00.836: ERROR/AndroidRuntime(418): java.lang.NullPointerException
我做得对吗?
我哪里错了?
非常感谢您的帮助。
I am trying to call a method available in a Activity
from a java file.
The calling is made fine and the control transfers to the function by this code in the java file.
myActivity my = new myActivty();
myActivity.method1();
Now my problem is i cant able to give a toast
message or display the alertdialog
, Its giving null pointer exception when im using the Toast
message,
07-20 15:13:00.836: ERROR/AndroidRuntime(418): java.lang.NullPointerException
Am i doing right?
Where am i wrong?
Help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Toast 需要上下文才能显示。您正在尝试显示 toast,但您创建的 Activity 没有上下文。传递工作 Activity 的实例来创建对话框或显示 toast。
Toast needs Context to show up. You're trying to show toast but you have created Activity without Context. Pass instance of working Activity to create dialog or show toast.
:) 这行不通。您需要将某种类型的引用从您的活动传递到其他 Java 类。
更多内容请参见这里 http://groups.google.com /group/android-developers/browse_thread/thread/741caff5a9536859?pli=1
:) This wont work. You will need to pass some kind of a reference from your activity to the other Java class.
More in this here http://groups.google.com/group/android-developers/browse_thread/thread/741caff5a9536859?pli=1