[英]android call method from another class nullpointerexception
I have a activty which name is HelperActivity.class
and I have another class which extends SherlockFragment. 我有一个名为
HelperActivity.class
,还有另一个扩展了SherlockFragment的类。 SherlockFragment have three methods playAudio()
, pauseAudio()
, stopAudio();
SherlockFragment具有三种方法
playAudio()
, pauseAudio()
, stopAudio();
like this; 像这样;
public void playAudio() {
mediaPlayer.start();
btn_play.setEnabled(false);
pDialog.dismiss();
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}
I want to call this method from HelperActivty.class
. 我想从
HelperActivty.class
调用此方法。 I tried many way but I got NullPointerException
every time.I tried to send context and tried change my methods to static but result same.Please help. 我尝试了很多方法,但每次都遇到
NullPointerException
我尝试发送上下文,并尝试将我的方法更改为static但结果相同。请帮助。 First class 头等舱
public class HelperActivity extends Activity
second class; 二等
public class RadioStream extends SherlockFragment implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener
The error; 错误;
FATAL EXCEPTION: main
01-28 02:40:50.390: E/AndroidRuntime(2915): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fedorvlasov.mostban/com.fedorvlasov.mostban.HelperActivity}: java.lang.NullPointerException
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread.access$700(ActivityThread.java:158)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.os.Looper.loop(Looper.java:176)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread.main(ActivityThread.java:5365)
01-28 02:40:50.390: E/AndroidRuntime(2915): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 02:40:50.390: E/AndroidRuntime(2915): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 02:40:50.390: E/AndroidRuntime(2915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-28 02:40:50.390: E/AndroidRuntime(2915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-28 02:40:50.390: E/AndroidRuntime(2915): at dalvik.system.NativeStart.main(Native Method)
01-28 02:40:50.390: E/AndroidRuntime(2915): Caused by: java.lang.NullPointerException
01-28 02:40:50.390: E/AndroidRuntime(2915): at com.fedorvlasov.mostban.RadioStream.playAudio(RadioStream.java:310)
01-28 02:40:50.390: E/AndroidRuntime(2915): at com.fedorvlasov.mostban.HelperActivity.onCreate(HelperActivity.java:52)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.Activity.performCreate(Activity.java:5326)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
01-28 02:40:50.390: E/AndroidRuntime(2915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
01-28 02:40:50.390: E/AndroidRuntime(2915): ... 11 more
01-28 02:40:52.054: I/Process(2915): Sending signal. PID: 2915 SIG: 9
Be sure that, wherever you call this method in this class, your mediaPlayer
object is already initialized. 确保无论在此类中在何处调用此方法,
mediaPlayer
对象mediaPlayer
初始化。 If you want to see the problem by yourself, place a breakpoint in the first line of your code and see that the object is null. 如果您想自己查看问题,请在代码的第一行中放置一个断点,然后查看该对象是否为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.