我有一个适用于Samsung S7和Tribute的应用程序。我最近在三星S5上尝试过它并且它崩溃了。我明白了:
[ 11-19 13:08:57.067 3463: 4039 D/ ]
HostConnection::get() New Host Connection established 0x9e587a40, tid 4039
11-19 13:08:57.156 3463-4039/com.example.android.pneumoniaapp I/OpenGLRenderer: Initialized EGL, version 1.4
11-19 13:08:57.156 3463-4039/com.example.android.pneumoniaapp D/OpenGLRenderer: Swap behavior 1
11-19 13:09:02.117 3463-3463/com.example.android.pneumoniaapp D/AndroidRuntime: Shutting down VM
11-19 13:09:02.118 3463-3463/com.example.android.pneumoniaapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.pneumoniaapp, PID: 3463
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.RuntimeException: setAudioSource failed.
at android.media.MediaRecorder.setAudioSource(Native Method)
at com.example.android.pneumoniaapp.Main_Activity.Recordbutton(Main_Activity.java:148)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
当我按下录制按钮时崩溃了。该按钮使用audioRecord和加速度计记录听觉信息。在Tribute和SAMSUNG S7上,整个代码顺利运行。
public void Recordbutton(View view) {
if (recordbuttonstatus) {
playBtn.setEnabled(true);
recordbuttonstatus = false;
startBtn.setText(getString(R.string.stoprecording));
sensorManager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
Thread accelerometerrecord = new Thread(new Runnable() {
@Override
public void run(){
File accoutputFile = new File(Environment.getExternalStorageDirectory(), "accelerometervals.txt");
try {
accoutputFile.createNewFile();
} catch (IOException e){
e.printStackTrace();
}
try {
writer = new FileWriter(accoutputFile, false); /*When false, the file is overwritten, when true, the file is appended.*/
} catch (IOException e) {
Log.e(LOG_TAG2, "failed");
}
}
});
accelerometerrecord.start();
Thread recordThread = new Thread(new Runnable() {
@Override
public void run() {
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(samplingFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
samplingFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize);
audioRecord.startRecording();
while (!recordbuttonstatus) {
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for (int i = 0; i < numberOfShort; i++) {
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
recordThread.start();
} else {
startBtn.setText(getString(R.string.newrecording));
recordbuttonstatus = true;
sensorManager.unregisterListener(accelListener);
}
}
答案 0 :(得分:0)
检查AndroidManifest.xml
以获取这些权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并在您的活动处理marshmallow的运行时权限。
private int REQUEST_CODE = 1;
在SD卡上写入文件之前,请检查录制音频权限和写入外部存储权限。
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO}, REQUEST_CODE);
}else {
//Write Data to SD Card
}
并覆盖onRequestPermission
活动方法。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED||grantResults[1] != PackageManager.PERMISSION_GRANTED) {
this.finish();
Toast.makeText(this, "Permission is required.", Toast.LENGTH_LONG).show();
}
return;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}