简体   繁体   中英

android call method from another class nullpointerexception

I have a activty which name is HelperActivity.class and I have another class which extends SherlockFragment. SherlockFragment have three methods 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 . 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM