2011-11-17 94 views
1

我是android開發新手。我正在嘗試製作一個簡單的鼓聲應用程序,當您點擊特定的鼓時,它會播放特定的聲音,但尚未完成,但我無法理解它爲什麼會一直崩潰。請幫幫我。下面的代碼:Android應用程序無故崩潰

package com.android.stiggpwnz.drumset; 

    import android.app.Activity; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ImageView; 

    public class DrumSetActivity extends Activity { 
     /** Called when the activity is first created. */ 
     MediaPlayer ridesound = MediaPlayer.create(this, R.raw.ridecymbal); 
     MediaPlayer hatsound = MediaPlayer.create(this, R.raw.highhat); 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      ImageView ride=(ImageView)findViewById(R.id.upLeftPlate); 
      ride.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        ridesound.start(); 
       } 
      }); 

      ImageView highHat=(ImageView)findViewById(R.id.midLeftPlate); 
      highHat.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        hatsound.start(); 
       } 
      }); 
     } 
    } 

這是我的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/drumset" > 

    <ImageView 
     android:id="@+id/upLeftPlate" 
     android:layout_width="135dp" 
     android:layout_height="55dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="31dp" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/kh" /> 

    <ImageView 
     android:id="@+id/midLeftPlate" 
     android:layout_width="117dp" 
     android:layout_height="30dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/upLeftPlate" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="22dp" 
     android:src="@drawable/kh" /> 

</RelativeLayout> 

這裏的logcat的說什麼:

11-17 20:55:56.349: E/AndroidRuntime(514): FATAL EXCEPTION: main 
11-17 20:55:56.349: E/AndroidRuntime(514): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.stiggpwnz.drumset/com.android.stiggpwnz.drumset.DrumSetActivity}: java.lang.NullPointerException 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.os.Looper.loop(Looper.java:123) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-17 20:55:56.349: E/AndroidRuntime(514): at java.lang.reflect.Method.invokeNative(Native Method) 
11-17 20:55:56.349: E/AndroidRuntime(514): at java.lang.reflect.Method.invoke(Method.java:521) 
11-17 20:55:56.349: E/AndroidRuntime(514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-17 20:55:56.349: E/AndroidRuntime(514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-17 20:55:56.349: E/AndroidRuntime(514): at dalvik.system.NativeStart.main(Native Method) 
11-17 20:55:56.349: E/AndroidRuntime(514): Caused by: java.lang.NullPointerException 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.media.MediaPlayer.create(MediaPlayer.java:641) 
11-17 20:55:56.349: E/AndroidRuntime(514): at com.android.stiggpwnz.drumset.DrumSetActivity.<init>(DrumSetActivity.java:12) 
11-17 20:55:56.349: E/AndroidRuntime(514): at java.lang.Class.newInstanceImpl(Native Method) 
11-17 20:55:56.349: E/AndroidRuntime(514): at java.lang.Class.newInstance(Class.java:1429) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
11-17 20:55:56.349: E/AndroidRuntime(514): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 
11-17 20:55:56.349: E/AndroidRuntime(514): ... 11 more 

並能有人告訴我,爲什麼需要這麼長的時間上傳我的模擬器上的90KB應用程序?

[2011-11-17 21:10:36 - DrumSet] ------------------------------ 
[2011-11-17 21:10:36 - DrumSet] Android Launch! 
[2011-11-17 21:10:36 - DrumSet] adb is running normally. 
[2011-11-17 21:10:36 - DrumSet] Performing com.android.stiggpwnz.drumset.DrumSetActivity activity launch 
[2011-11-17 21:10:36 - DrumSet] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'bulat' 
[2011-11-17 21:10:36 - DrumSet] Uploading DrumSet.apk onto device 'emulator-5554' 
[2011-11-17 21:13:37 - DrumSet] Installing DrumSet.apk... 
[2011-11-17 21:13:51 - DrumSet] Success! 
[2011-11-17 21:13:51 - DrumSet] Starting activity com.android.stiggpwnz.drumset.DrumSetActivity on device emulator-5554 
[2011-11-17 21:13:54 - DrumSet] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.stiggpwnz.drumset/.DrumSetActivity } 

上傳和安裝90KB的東西之間的3 minuts。爲什麼?

+1

logcat說什麼? – 2011-11-17 17:39:36

+0

它說很多東西,我沒有得到它,我怎麼能發送日誌給你? –

+0

按紅色的'E'按鈕看紅色的文字,它實際上顯示類,方法和線條,甚至試圖說出什麼樣的錯誤發生,所以它是工具#1使用 – 2011-11-17 17:48:53

回答

1

看看here, 注意下面的評論,這可能是你的問題。您沒有遵循媒體播放器的正確調用層次結構。 瀏覽媒體播放器的state diagram

這裏是完整的媒體播放器(播放音頻)example

希望這會有所幫助。

+0

添加一個答案,只是說另一個答案是正確的,不會添加任何其他內容有什麼意義?你應該有upvoted他,而不是 – Guillaume

+0

它沒有幫助:( –

+0

男子我很機械的失望.. –

1

logCat表示嘗試初始化您的活動時會引發異常。原因是從MediaPlayer獲取資源時有NullPOinterException

對我而言,這意味着R.raw.ridecymbalR.raw.highhat中的任一個不存在。檢查他們的案件,並確保他們真的存在(如果我正確地計算線路,可能是第一個:它說12行)。

+0

但他們確實存在,我可以玩並聽到他們...... –

+0

然後你有沒有試過在setContentView行之後移動你的activity的onCreate方法中的兩個MediaPLayer行? – Guillaume

+0

是的,我做到了,但它一直崩潰 –

1

你可以嘗試將這些二:

MediaPlayer ridesound = MediaPlayer.create(this, R.raw.ridecymbal); 
MediaPlayer hatsound = MediaPlayer.create(this, R.raw.highhat); 

是後的setContentView();

編輯: 這個問題可能是您正在嘗試引用的MediaPlayer之前,它實際上是由活動本身引用,或您要設置一些結果還未被引用的組件

+0

你是什麼意思?即時通訊不試圖做任何非法:D 和是啊,我移動這些,它不斷崩潰 除了上面的代碼 –

+0

好吧,我試着佈局在我的比較。一切正常,所以我假設問題是與MediaPlayer參考 – 2011-11-17 19:40:30

-1

您必須對創建的onCreate在MediaPlayer沒有以前那麼改變這兩條線:

 MediaPlayer ridesound; 
     MediaPlayer hatsound; 

,並在你的onCreate實例化的MediaPlayer

 ridesound = MediaPlayer.create(this, R.raw.ridecymbal); 
    hatsound = MediaPlayer.create(this, R.raw.highhat); 

你已經更好地遵循androidtutorial的mediaplayer,因爲你必須重寫所有的代碼... http://developer.android.com/guide/topics/media/mediaplayer.html