2013-05-13 113 views
-1

這裏的報告:顯示java.lang.NullPointerException ....崩潰報告

java.lang.RuntimeException: Unable to start activity ComponentInfo{rs.androidaplikacije.zastaveigradovi/rs.androidaplikacije.zastaveigradovi.PogresanOdgovor}: java.lang.NullPointerException 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3687) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at rs.androidaplikacije.zastaveigradovi.PogresanOdgovor.onCreate(PogresanOdgovor.java:56) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
... 11 more 

這基本上是一個彈出式的活動,說:「錯誤的答案」用一些聲音。下面是類:

public class PogresanOdgovor extends Activity{ 
    MediaPlayer zvuk; 
    TextView pogresno; 
    String tacanOdgovor; 
    String tacno; 

    public final int delayTime = 2500; 
    private Handler myHandler = new Handler(); 

    public void onUserInteraction(){ 
     myHandler.removeCallbacks(zatvoriPopup); 
     myHandler.postDelayed(zatvoriPopup, delayTime); 
    } 
    private Runnable zatvoriPopup = new Runnable(){ 
     public void run(){ 
      finish(); 
     } 
    }; 

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

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.pogresno); 

     Bundle extras = getIntent().getExtras(); 
     if(extras !=null) { 
      tacno = extras.getString("tacanOdgovor"); 
     } 

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
     boolean zvuci = getPrefs.getBoolean("checkbox", true); 

     myHandler.postDelayed(zatvoriPopup, delayTime); 

     inicijalizujVarijable(); 

     if(zvuci == true){ 
      zvuk.start(); 
      } 

      } 

    private void inicijalizujVarijable() { 
     Typeface pogresanFont = Typeface.createFromAsset(getAssets(), "Bebas.ttf"); 
     zvuk = MediaPlayer.create(this, R.raw.aah); 

     pogresno = (TextView) findViewById(R.id.tvPogresno); 

     pogresno.setTypeface(pogresanFont); 
     pogresno.setText("Pogrešan odgovor!\nTačan odgovor je:\n\n" + tacno); 

    } 
    } 

錯誤的ON LINE:

zvuk.start(); 

這正是我打電話給我的sound.I不明白這一點。如果有時有效,爲什麼不總是?

+0

zvuk.start(); ... – marjanbaz 2013-05-13 16:55:23

回答

0

事後您的MediaPlayer有沒有released

+0

你可以看到我上面的所有代碼。我不知道我應該釋放它。我什麼時候應該釋放它? – marjanbaz 2013-05-13 17:02:25

+0

使用setOnCompletionListener()安裝一個監聽器,通知您何時播放媒體,您可以釋放它。如果過早停止()播放並且不想繼續播放,則另外釋放它。 – 2013-05-13 17:17:43

+0

試過了,仍然收到錯誤報告。我不明白...我怎麼一會兒就得到這個錯誤,而不是所有的時間?如果我的代碼中有錯誤,它應該會一直崩潰。 – marjanbaz 2013-05-14 18:05:23