Flutter WebView修改时间输入致应用崩溃,求解决方案
Flutter WebView 修改时间输入字段崩溃及时间显示格式异常
我使用webview_flutter包在Flutter应用中展示自定义HTML页面,页面包含若干hh:mm格式的输入表单,已为所有字段设置默认值。但修改任意时间字段时应用会崩溃,即使注释了读取这些字段的唯一函数,问题仍存在。
崩溃错误日志
I/mple.dive_calc2(11766): Compiler allocated 4206KB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int) D/CompatChangeReporter(11766): Compat change id reported: 170233598; UID 10198; state: ENABLED D/AndroidRuntime(11766): Shutting down VM E/AndroidRuntime(11766): FATAL EXCEPTION: main E/AndroidRuntime(11766): Process: com.example.dive_calc2, PID: 11766 E/AndroidRuntime(11766): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.hasFocus()' on a null object reference
读取时间字段的函数(注释后问题仍存在)
// Function to calculate the time difference in hours and minutes between two fields async function calculateTimeDifference(startInputId, endInputId) { const startTime = await document.getElementById(startInputId).value; const endTime = await document.getElementById(endInputId).value; // Check if the values are available if (startTime && endTime) { const startDate = new Date(`1970-01-01T${startTime}:00`); const endDate = new Date(`1970-01-01T${endTime}:00`); // Calculate the total difference in minutes const totalMinutes = Math.abs((endDate - startDate) / 60000); // Convert total minutes into hours and minutes const hours = Math.floor(totalMinutes / 60); const minutes = totalMinutes % 60; // Display the result in the console (or it can be used to generate a logbook) console.log(`Time difference between ${startInputId} and ${endInputId}: ${hours} hours and ${minutes} minutes`); // Return the formatted time (can be used in a PDF or logbook) return { hours, minutes }; } else { console.log(`One or both fields (${startInputId}, ${endInputId}) are empty.`); return null; } }
已尝试的解决方案(均无效)
- 移除读取hh:mm字段的函数
- 将页面部署到Web端(Web端运行正常)
- 仅在
onPageFinished回调中运行该函数
额外问题
Android模拟器设置为24小时制,但HTML页面在应用内加载后默认显示12小时制(带AM/PM)。
内容的提问来源于stack exchange,提问作者nickksoares
相关产品推荐
相关产品推荐

