我正在开发一个Android应用程序,并且在任何地方都没有错误,但是当我在手机上运行它时,在应用程序打开后我点击一个按钮,过了一会儿它突然关闭并给了我消息“遗憾的是**已经停止了”。
有人可以告诉我出了什么问题吗?
我做了一些研究,发现当清单中有错误时会出现此消息。这是我的清单,我无法弄清楚出了什么问题?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tryagain"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
</manifest>
这是我的.java:
public class MainActivity extends Activity {
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder = null;
private Thread recordingThread = null;
private boolean isRecording = false;
ArrayList <Short> x=new ArrayList<Short>();
ArrayList <Short> y=new ArrayList<Short>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setButtonHandlers();
enableButtons(false);
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
//why is the following statement here???
System.out.println("BUFFER SIZE VALUE IS " + bufferSize);
}
private void setButtonHandlers() {
((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);
((Button) findViewById(R.id.Start2)).setOnClickListener(btnClick2);
((Button) findViewById(R.id.Stop2)).setOnClickListener(btnClick2);
}
private void enableButton(int id, boolean isEnable) {
((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.btnStart, !isRecording);
enableButton(R.id.btnStop, isRecording);
enableButton(R.id.Start2, !isRecording);
enableButton(R.id.Stop2, isRecording);
}
int BufferElements2Rec = 1024;
int BytesPerElement = 2; // 2 bytes in 16bit format
private void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private void startRecording2() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile2();
}
}, "AudioRecorder Thread");
recordingThread.start();
}
private byte[] short2byte(short[] sData) {
int shortArrsize = sData.length;
byte[] bytes = new byte[shortArrsize * 2];
for (int i = 0; i < shortArrsize; i++) {
bytes[i * 2] = (byte) (sData[i] & 0x00FF);
bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
sData[i] = 0;
}
return bytes;
}
private void writeAudioDataToFile() {
// Write the output audio in byte
String filePath = "/storage/sdcard0/Sounds/recording1.pcm";
short sData[] = new short[BufferElements2Rec];
// to convert sData from short to Short:
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format and stores it in buffer sData
recorder.read(sData, 0, BufferElements2Rec);
// here u will write the new code: to store what was said in an array,,, here we are
Short sData1= sData[BufferElements2Rec];
int i=0;
x.add(i,sData1);
i++;
System.out.println("Short wirting to file" + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} //END OF WRITE TO FILE METHOD
private void writeAudioDataToFile2() {
// Write the output audio in byte
String filePath = "/storage/sdcard0/Sounds/recording2.pcm";
short sData[] = new short[BufferElements2Rec];
// to convert sData from short to Short:
FileOutputStream os = null;
try {
os = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (isRecording) {
// gets the voice output from microphone to byte format and stores it in buffer sData
recorder.read(sData, 0, BufferElements2Rec);
// here u will write the new code: to store what was said in an array,,, here we are
Short sData1= sData[BufferElements2Rec];
int i=0;
y.add(i,sData1);
i++;
System.out.println("Short wirting to file" + sData.toString());
try {
// // writes the data to file from buffer
// // stores the voice buffer
byte bData[] = short2byte(sData);
os.write(bData, 0, BufferElements2Rec * BytesPerElement);
}
catch (IOException e) {
e.printStackTrace();
}
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} //END OF WRITE TO FILE METHOD
private void stopRecording() {
// stops the recording activity
if (null != recorder) {
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
}
private View.OnClickListener btnClick = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart: {
enableButtons(true);
startRecording();
break;
}
case R.id.btnStop: {
enableButtons(false);
stopRecording();
break;
}
}
}
};
private View.OnClickListener btnClick2 = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.Start2: {
enableButtons(true);
startRecording2();
break;
}
case R.id.Stop2: {
enableButtons(false);
stopRecording();
break;
}
}
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
这是我的logCat
06-27 16:50:46.830: I/System.out(11939): BUFFER SIZE VALUE IS 2048
06-27 16:50:46.980: D/libEGL(11939): loaded /vendor/lib/egl/libGLES_vc4.so
06-27 16:50:46.980: D/(11939): SpyHook inactive - could not find libspyhook.so [Symbol not found: ] or not enabled (debug.egl.hw.spy = 0)
06-27 16:50:47.000: W/khrn_client(11939): init_window num_buffers 3 min undequeued buffers 1 type 1
06-27 16:50:47.000: W/khrn_client(11939): init_window window 0x50eda0d8, 480x800 hintTransform 0x0
06-27 16:50:47.030: D/OpenGLRenderer(11939): Enabling debug mode 0
06-27 16:50:53.066: W/dalvikvm(11939): threadid=12: thread exiting with uncaught exception (group=0x40fb72a0)
06-27 16:50:53.066: E/AndroidRuntime(11939): FATAL EXCEPTION: AudioRecorder Thread
06-27 16:50:53.066: E/AndroidRuntime(11939): java.lang.ArrayIndexOutOfBoundsException: length=1024; index=1024
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.access$4(MainActivity.java:125)
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity$3.run(MainActivity.java:84)
06-27 16:50:53.066: E/AndroidRuntime(11939): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
那么, 这些行
06-27 16:50:53.066: E/AndroidRuntime(11939): java.lang.ArrayIndexOutOfBoundsException: length=1024; index=1024
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
告诉您,在第148行,您尝试在长度为1024的数组中访问索引号1024。
您应该始终运行length - 1
检查第148行(如果你不明白那里的错误,可以在评论中发布)。
祝你好运答案 1 :(得分:0)
这不是一个直接的答案,但会帮助您阅读您的logcat并自行调试并发布大多数相关代码,以便我们可以更有效地帮助并防止downvotes和关闭
如果您在
之后查找该行 FATAL EXCEPTION: AudioRecorder Thread
它会给你错误
06-27 16:50:53.066: E/AndroidRuntime(11939): java.lang.ArrayIndexOutOfBoundsException: length=1024; index=1024
在这里,您尝试访问Array
中的索引,该索引是它的长度(您将获得Array
长度和index
,而您最终会尝试访问该索引。因为我们知道ArrayIndexOutOfBoundsException
以Arrays
为0开始,所以它会抛出index
。然后找到引用代码的第一行
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
这告诉我们MainActivity
函数中writeAudioDataToFile()
的第148行发生异常
答案 2 :(得分:0)
在阅读了@codeMagic的内容之后,我读了LogCat,然后我加倍点击
06-27 16:50:53.066: E/AndroidRuntime(11939): at com.example.tryagain.MainActivity.writeAudioDataToFile(MainActivity.java:148)
它告诉我问题在哪里
Short sData1= sData[BufferElements2Rec];
然后我用
替换它Short sData1= sData[i];
感谢所有帮助过我的人。