当我尝试打开此页面时,它会显示应用程序意外停止的错误

时间:2014-02-23 17:38:02

标签: android eclipse media-player

代码

Button play,pause;
final MediaPlayer sng = MediaPlayer.create(this, R.raw.atlas123);

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.anthem);

    play = (Button)findViewById(R.id.btnplay);
     pause = (Button)findViewById(R.id.btnpause);

    play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sng.start();
        }
    });

    pause.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            sng.stop();
            //sng.release();
        }
    });
}
}

错误:

02-23 22:58:26.923: E/AndroidRuntime(1367): FATAL EXCEPTION: main
02-23 22:58:26.923: E/AndroidRuntime(1367): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.srm.sarthak/com.example.srm.sarthak.Anthem}: java.lang.NullPointerException
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.os.Looper.loop(Looper.java:130)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at java.lang.reflect.Method.invoke(Method.java:507)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at dalvik.system.NativeStart.main(Native Method)
02-23 22:58:26.923: E/AndroidRuntime(1367): Caused by: java.lang.NullPointerException
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.media.MediaPlayer.create(MediaPlayer.java:662)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at com.example.srm.sarthak.Anthem.<init>(Anthem.java:13)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at java.lang.Class.newInstanceImpl(Native Method)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at java.lang.Class.newInstance(Class.java:1409)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-23 22:58:26.923: E/AndroidRuntime(1367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-23 22:58:26.923: E/AndroidRuntime(1367):     ... 11 more

2 个答案:

答案 0 :(得分:2)

您尝试在此Context之前将活动用作onCreate()

final MediaPlayer sng = MediaPlayer.create(this, R.raw.atlas123);

更改为

final MediaPlayer sng;

并将初始化移至onCreate()

sng = MediaPlayer.create(this, R.raw.atlas123);

答案 1 :(得分:1)

将MediaPlayer.create()移动到on create function。