嘿,我在这里关注有关soundpool的教程http://www.techrepublic.com/blog/app-builder/getting-your-feet-wet-in-androids-soundpool/877
但在这样做之后,我的应用程序无法运行并说“不幸的是app_name已停止”
需要帮助,对android和eclipse以及任何编码都是新手
Main.java
package com.example.touchscreenexperiment;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
private SoundPool mSoundPool;
private AudioManager mAudioManager;
private HashMap<Integer, Integer> mSoundPoolMap;
private int mStream1 = 0;
private int mStream2 = 0;
final static int LOOP_1_TIME = 0;
final static int LOOP_3_TIMES = 2;
final static int SOUND_FX_01 = 1;
final static int SOUND_FX_02 = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up our audio player
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mSoundPoolMap = new HashMap();
//load fx
mSoundPoolMap.put(SOUND_FX_01, mSoundPool.load(this, R.raw.set_trap, 1));
mSoundPoolMap.put(SOUND_FX_02, mSoundPool.load(this, R.raw.spring_trap, 1));
//wire buttons
Button b = (Button)findViewById(R.id.fx01);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.fx02);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.stop);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
switch (v.getId()) {
case R.id.fx01:
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(SOUND_FX_01), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
break;
case R.id.fx02:
mSoundPool.stop(mStream2);
mStream2= mSoundPool.play(mSoundPoolMap.get(SOUND_FX_02), streamVolume, streamVolume, 1, LOOP_3_TIMES, 1f);
break;
case R.id.stop:
mSoundPool.stop(mStream1);
mSoundPool.stop(mStream2);
break;
}
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sound FX Demo"
android:textSize="24sp"
android:gravity="center"
android:textColor="#ffffff"
android:paddingBottom="20dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="8dip" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play #1"
android:id="@+id/fx01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play #2"
android:id="@+id/fx02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop All"
android:id="@+id/stop"/>
</LinearLayout>
</LinearLayout>
这是我的logcat
05-30 20:01:41.560: D/AndroidRuntime(24402): Shutting down VM
05-30 20:01:41.560: W/dalvikvm(24402): threadid=1: thread exiting with uncaught exception (group=0x40be4930)
05-30 20:01:41.560: E/AndroidRuntime(24402): FATAL EXCEPTION: main
05-30 20:01:41.560: E/AndroidRuntime(24402): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.touchscreenexperiment/com.example.touchscreenexperiment.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.touchscreenexperiment.MainActivity" on path: /data/app/com.example.touchscreenexperiment-2.apk
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.os.Looper.loop(Looper.java:137)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 20:01:41.560: E/AndroidRuntime(24402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 20:01:41.560: E/AndroidRuntime(24402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 20:01:41.560: E/AndroidRuntime(24402): at dalvik.system.NativeStart.main(Native Method)
05-30 20:01:41.560: E/AndroidRuntime(24402): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.touchscreenexperiment.MainActivity" on path: /data/app/com.example.touchscreenexperiment-2.apk
05-30 20:01:41.560: E/AndroidRuntime(24402): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-30 20:01:41.560: E/AndroidRuntime(24402): ... 11 more
非常感谢
答案 0 :(得分:0)
检查您的清单。我相信,您在清单中将活动宣布为.MainActivity
,但您的班级只是Main
。
编辑:具体来说,在您的AndroidManifest.xml文件中,您将拥有以下内容:
<activity
android:name=".MainActivity"
android:label="@string/app_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
请注意android:name=".MainActivity"
行。这告诉应用程序类名是MainActivity
。在您的情况下,类名称为Main
,因此该行需要更改为android:name=".Main"
,您应该没问题。