我是android开发的新手。我正在尝试制作一个简单的drumset应用程序,当你点击特定的鼓,它播放特定的声音,它还没有完成,但我不明白为什么它一直崩溃。请帮我。这是代码:
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分钟。为什么呢?
答案 0 :(得分:1)
logCat表示在尝试初始化您的活动时会引发异常。从NullPOinterException
获取资源时,原因是MediaPlayer
。
对我而言,这意味着R.raw.ridecymbal
或R.raw.highhat
中的任何一个都不存在。检查他们的情况,并确保他们确实存在(如果我正确计算线路,可能是第一个:它表示第12行)。
答案 1 :(得分:1)
你能尝试移动这两个:
MediaPlayer ridesound = MediaPlayer.create(this, R.raw.ridecymbal);
MediaPlayer hatsound = MediaPlayer.create(this, R.raw.highhat);
在 setContentView();
之后修改强> 问题可能是您在活动本身实际引用之前尝试引用MediaPlayer,或者您尝试将某些结果设置为尚未引用的组件
答案 2 :(得分:1)
看看here, 注意下面的评论,这可能是你的问题。您没有遵循媒体播放器的正确调用层次结构。 浏览媒体播放器的state diagram。
这里是媒体播放器的完整example(播放音频)。
希望这有帮助。
答案 3 :(得分:-1)
您必须先在onCreate上创建MediaPlayer,因此请更改以下两行:
MediaPlayer ridesound;
MediaPlayer hatsound;
并在onCreate上实例化MediaPlayer
ridesound = MediaPlayer.create(this, R.raw.ridecymbal);
hatsound = MediaPlayer.create(this, R.raw.highhat);
你最好遵循mediaplayer的androidtutorial,因为你必须重写所有代码...... http://developer.android.com/guide/topics/media/mediaplayer.html