gpt4 book ai didi

java - 安卓工作室。仅使用 onTouchListener 单击按钮/ ImageView 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 10:56:51 24 4
gpt4 key购买 nike

您好,我正在开发一个可以跟踪每个用户位置的应用程序,作为其中的一部分,该应用程序可以在将语音消息发送到 mysql 服务器之前录制语音消息。当用户按住录制按钮/ ImageView 时,该应用程序运行良好并运行。但是,当某些用户仅单击按钮/ ImageView 而不按住它来录制声音时,会发生崩溃。

如何防止此应用程序崩溃?如果用户仅单击按钮并发出警告但不使应用程序崩溃,我该如何处理。谢谢

mFileName = Environment.getExternalStorageDirectory() + "/"+user_id+".3gp";
recordBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {


if(checkPermissionFromDevice()){

if(event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
recordLabel.setText("Recording Started...");
}else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
recordLabel.setText("Recording Stoped...");
}

}
else{
requestPermission();
}
return false;
}
});


}

日志猫:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: www.teamruby.com.capstoneproject, PID: 3679
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at www.teamruby.com.capstoneproject.INeedHelp.stopRecording(INeedHelp.java:383)
at www.teamruby.com.capstoneproject.INeedHelp.access$300(INeedHelp.java:69)
at www.teamruby.com.capstoneproject.INeedHelp$3.onTouch(INeedHelp.java:194)
at android.view.View.dispatchTouchEvent(View.java:9290)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9514)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5857)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6025)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(Me

最佳答案

我认为错误实际上发生是因为用户立即开始录制并很快停止,因此媒体录制器可能在启动之前就停止了,因为它说 MediaRecorder.stop 失败。现在一个简单的解决方案是将其包装在 try catch 中,如果发生错误,则显示警告 toast,如下所示:

try {
stopRecording();
} catch(RunTimeException e) {
e.printstack();
//your toast
}

关于java - 安卓工作室。仅使用 onTouchListener 单击按钮/ ImageView 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51662747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com