我有一个条目,该条目收集一个以后用作双精度数的数字。 一切正常,直到一个人删除条目中的所有数字以输入一个新数字为止。
我尝试通过在Text为空时立即抛出新的Exception来修复它,但仍然会因
而崩溃输入字符串的格式不正确。 (FormatException)
错误发生在 public void execute() {
int numConsumers = Integer.parseInt(configs.getSINK_NUMBER_OF_CONSUMERs());
Future<?> future=null;
log.info("Creating {} consumers",numConsumers);
List<String> topics = Arrays.asList(configs.getTOPIC_NAME());
ExecutorService executor = Executors.newFixedThreadPool(numConsumers);
final List<SinkConsumer> consumers = new ArrayList<>();
for (int i = 0; i < numConsumers; i++) {
consumer = new SinkConsumer(UUID.randomUUID().toString(),topics,configs);
consumers.add(consumer);
future = executor.submit(consumer);
}
try {
future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
executor.shutdown();
}
}
这是我的xaml代码:
moneyOutput.Text = Convert.ToDouble(moneyInput.Text).ToString() + "€";
和我的xaml.cs代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bierrechner.Priceentry"
BackgroundColor="#8c1029"
Title="Eingabe">
<ScrollView>
<StackLayout Margin="0,30,0,0" >
<Entry x:Name="moneyInput" TextChanged="MoneyInput_TextChanged" Placeholder="z.B. 34,99" PlaceholderColor="Gray" TextColor="White" Keyboard="Numeric"/>
<Label x:Name="moneyOutput" Margin="0,40,0,0" HorizontalTextAlignment="Center" FontSize="Large" TextColor="Orange"/>
<Button Text="Weiter" Clicked="Button_Clicked" HorizontalOptions="Center" VerticalOptions="EndAndExpand"/>
</StackLayout>
</ScrollView>
我注意到private void MoneyInput_TextChanged(object sender, TextChangedEventArgs e)
{
if (moneyInput.Text != null)
{
moneyOutput.Text = Convert.ToDouble(moneyInput.Text).ToString() + "€";
sharePrice = Convert.ToDouble(moneyInput.Text);
}
else if (moneyInput.Text == null)
{
throw new ArgumentException("String cannot be null or empty");
}
}
无效,因为代码仍在执行。
答案 0 :(得分:1)
您的代码只需再次检查为空。
if (moneyInput.Text != null)
您还需要检查空字符串。
if (!string.IsNullOrEmpty(moneyInput.Text))
实际上,清除输入框后,Text属性不会为空,它只是一个空字符串。
也可以考虑使用Double.TryParse,而不是Convert.ToDouble。无论字符串的值是什么,TryParse方法都不会引发异常,如果字符串为null,空或只是格式错误,则该方法只会返回false。