应用程序意外停止

时间:2012-08-09 08:24:13

标签: android android-emulator

当用户点击按钮时,我正在尝试创建一个播放随机声音的简单应用。该应用程序启动没有问题,它会在崩溃之前发出随机数量的声音。我找不到任何类似的问题/问题,这就是我发布这个问题的原因。

这是我的日志:

08-09 10:10:16.706: D/dalvikvm(265): GC freed 571 objects / 49008 bytes in 128ms
08-09 10:10:18.896: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.036: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.036: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.036: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.236: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.336: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.346: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.346: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.746: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.856: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.016: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.166: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.166: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.166: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.396: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.556: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.566: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.566: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.766: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.907: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.907: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.916: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.116: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:21.236: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.246: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:21.246: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.406: W/MediaPlayer(265): info/warning (1, 26)
08-09 10:10:21.427: E/MediaPlayer(265): error (1, -17)
08-09 10:10:21.447: D/MediaPlayer(265): create failed:
08-09 10:10:21.447: D/MediaPlayer(265): java.io.IOException: Prepare failed.: status=0x1
08-09 10:10:21.447: D/MediaPlayer(265):     at android.media.MediaPlayer.prepare(Native  Method)
08-09 10:10:21.447: D/MediaPlayer(265):     at     android.media.MediaPlayer.create(MediaPlayer.java:644)
08-09 10:10:21.447: D/MediaPlayer(265):     at  com.moer.playsound.MainActivity$1.onClick(MainActivity.java:40)
08-09 10:10:21.447: D/MediaPlayer(265):     at  android.view.View.performClick(View.java:2364)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.View.onTouchEvent(View.java:4179)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.View.dispatchTouchEvent(View.java:3709)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.j ava:1659)
08-09 10:10:21.447: D/MediaPlayer(265):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-09 10:10:21.447: D/MediaPlayer(265):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1 643)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.os.Looper.loop(Looper.java:123)
08-09 10:10:21.447: D/MediaPlayer(265):     at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 10:10:21.447: D/MediaPlayer(265):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:10:21.447: D/MediaPlayer(265):     at java.lang.reflect.Method.invoke(Method.java:521)
08-09 10:10:21.447: D/MediaPlayer(265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 10:10:21.447: D/MediaPlayer(265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 10:10:21.447: D/MediaPlayer(265):     at dalvik.system.NativeStart.main(Native Method)
08-09 10:10:21.466: D/AndroidRuntime(265): Shutting down VM
08-09 10:10:21.466: W/dalvikvm(265): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-09 10:10:21.476: E/AndroidRuntime(265): Uncaught handler: thread main exiting due to uncaught exception
08-09 10:10:21.496: E/AndroidRuntime(265): java.lang.NullPointerException
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.moer.playsound.MainActivity$1.onClick(MainActivity.java:41)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.View.performClick(View.java:2364)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.View.onTouchEvent(View.java:4179)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.View.dispatchTouchEvent(View.java:3709)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.os.Looper.loop(Looper.java:123)
08-09 10:10:21.496: E/AndroidRuntime(265):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 10:10:21.496: E/AndroidRuntime(265):  at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:10:21.496: E/AndroidRuntime(265):  at java.lang.reflect.Method.invoke(Method.java:521)
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 10:10:21.496: E/AndroidRuntime(265):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 10:10:21.496: E/AndroidRuntime(265):  at dalvik.system.NativeStart.main(Native Method)
08-09 10:10:21.516: I/dalvikvm(265): threadid=7: reacting to signal 3
08-09 10:10:21.726: I/dalvikvm(265): Wrote stack trace to '/data/anr/traces.txt'
08-09 10:10:34.156: I/Process(265): Sending signal. PID: 265 SIG: 9

这是代码:

package com.moer.playsound;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import java.util.Random;
import android.view.KeyEvent;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {


private final int NUM_SOUND_FILES = 8;

private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
private MediaPlayer mp;

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

mfile[0] = R.raw.sound1;  
mfile[1] = R.raw.sound2;  
mfile[2] = R.raw.sound3;
mfile[3] = R.raw.sound4;
mfile[4] = R.raw.sound5;
mfile[5] = R.raw.sound6;
mfile[6] = R.raw.sound7;
mfile[7] = R.raw.sound8;


Button button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
     mp = MediaPlayer.create(MainActivity.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
     mp.seekTo(0);
     mp.start(); 
   }});

} 
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        finish();
 return true;
  }
return false;
}
}   

我尝试使用wav格式并减少使用的文件数量,但它没有做任何事情。我是新手,所以任何帮助都将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:1)

嗨试试这个,

import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private static final int[] SOUNDS = new int[] { R.raw.sound1, R.raw.sound2,
            R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6,
            R.raw.sound7, R.raw.sound8 };

    // private int mfile[] = new int[NUM_SOUND_FILES];
    private Random rnd = new Random();
    private MediaPlayer mp;
    int flag = 0;

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

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                if (flag == 0) {
                    Log.v("Flag Values is", flag + "");
                    audioPlaying();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "player is currently Playing", Toast.LENGTH_LONG)
                            .show();
                    mp.stop();
                    mp.release();
                    flag = 0;
                    audioPlaying();
                }

            }
        });
        // Button button2 = (Button) findViewById(R.id.button2);
        // button2.setOnClickListener(new OnClickListener() {
        // public void onClick(View v) {
        //
        // mp.stop();
        // mp.release();
        //
        // }
        // });
    }

    protected void audioPlaying() {
        // TODO Auto-generated method stub
        int sndToPlay = rnd.nextInt(SOUNDS.length);
        mp = MediaPlayer.create(MainActivity.this, SOUNDS[sndToPlay]);
        mp.seekTo(0);
        mp.start();
        flag++;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            finish();
            return true;
        }
        return false;
    }

    @Override
    protected void onStop() {
        super.onStop();
        // deallocate all memory
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
            }
            mp.release();
            mp = null;
        }
    }
}

答案 1 :(得分:-2)

第41行代码的哪一部分为空?