0

我正在制作一个音板应用程序,但我不知道如何让我的声音播放。这是我的 MainActivity.java。我希望它在每个按钮上播放不同的声音。它还必须自动停止播放以前的声音。(我刚开始为 android 开发,所以我没有你可能注意到的那么好)。

在此先感谢您的帮助!

编辑:

这是我的java的新版本:

    package com.mega.soundboard;

    import java.io.IOException;
    import android.app.Activity;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.Resources;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;


    public class MainActivity extends Activity implements OnClickListener {
    MediaPlayer player = new MediaPlayer();


    int[] buttonIds = {R.id.btn1_1,R.id.btn1_2,R.id.btn1_3,R.id.btn1_4,R.id.btn1_5,R.id.btn1_6,R.id.btn1_7,R.id.btn1_8,R.id.btn1_9,R.id.btn1_10,R.id.btn1_11,R.id.btn1_12,R.id.btn1_13,R.id.btn1_14};
    int[] soundIds = {R.raw.sound01, R.raw.sound02, R.raw.sound03, R.raw.sound04, R.raw.sound05, R.raw.sound06, R.raw.sound07, R.raw.sound08, R.raw.sound09, R.raw.sound10, R.raw.sound11, R.raw.sound12, R.raw.sound13, R.raw.sound14, R.raw.sound15, R.raw.sound16};

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    public void onClick(View v) {

        //find the index that matches the button's ID, and then reset
        //the MediaPlayer instance, set the data source to the corresponding
        //sound effect, prepare it, and start it playing.
        for(int i = 0; i < buttonIds.length; i++) {
            if(v.getId() == buttonIds[i]) {
                AssetFileDescriptor afd = v.getContext().getResources().openRawResourceFd(soundIds[i]);
                    try {
                        player.reset();
                        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                        player.prepare();
                        player.start();
                        afd.close();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }

这是新的日志:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mega.soundboard/com.mega.soundboard.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
at com.mega.soundboard.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
... 11 more
Sending signal. PID: 16842 SIG: 9

这是我的布局文件:(它非常基本)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#232b09">

<RelativeLayout

  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="fill_parent">


  <!--  Screen Design for VIDEOS -->
    <Button
      android:id="@+id/btn1.1"
      android:layout_width="fill_parent"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:text="@string/button1.1"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.2"
      android:layout_width="fill_parent"
      android:text="@string/button1.2"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.1"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.3"
      android:layout_width="fill_parent"
      android:text="@string/button1.3"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.2"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.4"
      android:layout_width="fill_parent"
      android:text="@string/button1.4"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.3"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.5"
      android:layout_width="fill_parent"
      android:text="@string/button1.5"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.4"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.6"
      android:layout_width="fill_parent"
      android:text="@string/button1.6"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.5"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.7"
      android:layout_width="fill_parent"
      android:text="@string/button1.7"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.6"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.8"
      android:layout_width="fill_parent"
      android:text="@string/button1.8"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.7"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />

    <Button
      android:id="@+id/btn1.9"
      android:layout_width="fill_parent"
      android:text="@string/button1.9"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.8"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />      
    <Button
      android:id="@+id/btn1.10"
      android:layout_width="fill_parent"
      android:text="@string/button1.10"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.9"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />          
    <Button
      android:id="@+id/btn1.11"
      android:layout_width="fill_parent"
      android:text="@string/button1.11"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.10"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />  

    <Button
      android:id="@+id/btn1.12"
      android:layout_width="fill_parent"
      android:text="@string/button1.12"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.11"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />      
    <Button
      android:id="@+id/btn1.13"
      android:layout_width="fill_parent"
      android:text="@string/button1.13"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.12"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />          
    <Button
      android:id="@+id/btn1.14"
      android:layout_width="fill_parent"
      android:text="@string/button1.14"
      android:layout_height="50dp"
      android:background="#44511c" 
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/btn1.13"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
    />                  
</RelativeLayout>
</ScrollView>
4

2 回答 2

1
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:81)

resnull当你试图访问它时你得到的NullPointerException

Resources res = getResources();

你应该得到里面的资源,onCreate()因为getResources()需要context.

于 2013-05-12T15:50:03.980 回答
0

将您的资源对象分配移动到您的onCreate方法。在此之前,您的活动的上下文尚未通过 ContextWrapper 对象设置。

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = getResouces();
}

还要为布局文件中的每个按钮添加 onClick 属性以onClick在活动中调用该函数:

   <Button
  android:id="@+id/btn1.11"
  android:layout_width="fill_parent"
  android:text="@string/button1.11"
  android:layout_height="50dp"
  android:background="#44511c" 
  android:layout_centerHorizontal="true"
  android:layout_below="@+id/btn1.10"
  android:layout_marginTop="5dp"
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:onClick="onClick"
/>  
于 2013-05-12T15:53:21.677 回答