2014-02-23 42 views
0

代碼當我試圖打開這個網頁它給出了一個錯誤,該應用程序意外終止

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

你試圖使用活性onCreate()一個Context前在這裏:

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

更改爲

final MediaPlayer sng; 

和移動初始化onCreate()

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

移動的MediaPlayer.create()到你的上創建功能。

相關問題