I’m new at android developing. I’m trying to make a simple drumset app, when you click on specific drum, it plays specific sounds, it isnt finished yet, but I cant understand why is it crashing all the time. Please help me. Here’s the code:
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();
}
});
}
}
And here’s my layout:
<?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>
Here’s what LogCat say:
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
and can someone please tell me why is it taking so long to upload my 90KB app on the emulator?
[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 }
3 minuts between uploading and installing a 90KB thing. Why?
take a look here ,
notice the comment below, this may be your problem. Your are not following proper calling hierarchy of media player.
Go through the state diagram of media player.
And here is complete example of Media Player (Playing audio).
Hope this helps.