从另一个类nullpointerexception调用android调用方法

时间:2015-01-28 10:01:34

标签: java android class methods nullpointerexception

我有一个名为HelperActivity.class的活动,我有另一个扩展SherlockFragment的类。 SherlockFragment有三种方法playAudio()pauseAudio()stopAudio();

像这样;

public void playAudio() {

        mediaPlayer.start();

        btn_play.setEnabled(false);
        pDialog.dismiss();

        btn_pause.setEnabled(true);
        btn_stop.setEnabled(true);
    }

我想从HelperActivty.class调用此方法。我尝试了很多方法但每次都得到NullPointerException。我试图发送上下文并尝试将我的方法更改为静态但结果相同。请帮助。 头等舱

public class HelperActivity extends Activity

第二课;

public class RadioStream extends SherlockFragment implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener

错误;

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

1 个答案:

答案 0 :(得分:0)

请确保,无论您在此课程中何处调用此方法,您的mediaPlayer对象都已初始化。如果您想自己查看问题,请在代码的第一行放置一个断点,并看到该对象为null。