gpt4 book ai didi

java - 无法执行 android :onClick - Parsing String to Double 的方法

转载 作者:行者123 更新时间:2023-11-30 00:57:57 24 4
gpt4 key购买 nike

我正在尝试创建一个 Android 应用程序,但在单击按钮时出现错误。

我想做的是将字符串转换为 double 。我不能将文本字段用作数字,因为我正在处理纬度和经度,所以我需要负数,而 type="number"不允许这样做。

这是我执行的函数:

public void insertSite(View view) {
String type = ((AutoCompleteTextView)findViewById(R.id.type)).getText().toString();
String address = ((AutoCompleteTextView)findViewById(R.id.address)).getText().toString();
String city = ((AutoCompleteTextView)findViewById(R.id.city)).getText().toString();
String district = ((AutoCompleteTextView)findViewById(R.id.district)).getText().toString();
String cep = ((AutoCompleteTextView)findViewById(R.id.cep)).getText().toString();
String state = ((AutoCompleteTextView)findViewById(R.id.state)).getText().toString();
double latitude = Double.parseDouble(((EditText)findViewById(R.id.latitude)).getText().toString());
double longitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.longitude)).getText().toString());
double altitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.altitude)).getText().toString());
try {
VivoDbDAO dao = new VivoDbDAO(this);
dao.insertSite(
type,
address,
city,
district,
state,
cep,
latitude,
longitude,
altitude);
Toast.makeText(this, "Site 1 inserido", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, CadastroImovel2Activity.class);
startActivity(intent);
finish();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cadastroImovel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha=".9"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.thiago.muitogelo.CadastroActivity"
>

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="?attr/colorPrimary"
android:backgroundTint="@color/roxoVivo"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="Dr. Antena" />

</android.support.design.widget.AppBarLayout>

<Space
android:layout_width="wrap_content"
android:layout_height="40dp" />

<ScrollView
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center">

<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<AutoCompleteTextView
android:id="@+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Prédio ou Térreo?"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Endereço"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:inputType="phone"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Cidade"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/district"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:inputType="text"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Bairro"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:inputType="text"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Estado"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/cep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/text_field"
android:elevation="5dp"
android:inputType="number"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="CEP"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<EditText
android:id="@+id/latitude"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button2"
android:inputType="text"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Latitude"
android:textColor="@color/roxoEscuro" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/longitude"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button2"
android:inputType="text"
android:hint="Longitude"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:textColor="@color/roxoEscuro"
android:clickable="false" />

<Space
android:layout_width="match_parent"
android:layout_height="10dp" />

<AutoCompleteTextView
android:id="@+id/altitude"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button2"
android:inputType="text"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:hint="Altitude"
android:textColor="@color/roxoEscuro"
android:clickable="false" />
</LinearLayout>
</ScrollView>

<Space
android:layout_width="wrap_content"
android:layout_height="40dp" />

<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_gravity="center"
android:padding="10dp">


<Button
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@drawable/button2"
android:text="Próximo"
android:textColor="@color/roxoEscuro"
android:id="@+id/btConcluidoCadastro"
android:onClick="insertSite"
android:layout_weight="2"/>
</LinearLayout>
</LinearLayout>

这是日志:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thiago.muitogelo, PID: 334
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:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
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:5716) 
at android.widget.TextView.performClick(TextView.java:10926) 
at android.view.View$PerformClick.run(View.java:22596) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.thiago.muitogelo.CadastroImovel1Activity.insertSite(CadastroImovel1Activity.java:30)
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:5716) 
at android.widget.TextView.performClick(TextView.java:10926) 
at android.view.View$PerformClick.run(View.java:22596) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

最佳答案

日志显示您输入了一个无效的 double 。可能是问题出在

double latitude = Double.parseDouble(((EditText)findViewById(R.id.latitude)).getText().toString());
double longitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.longitude)).getText().toString());
double altitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.altitude)).getText().toString());

即。纬度、经度、高度中的值可能包含无效的 double 值。如果该字段中没有值可能会导致此错误。使用此。

if(((EditText)findViewById(R.id.latitude)).getText().toString().length()!=0&&((EditText)findViewById(R.id.longitude))).getText().toString().length()!=0&&((EditText)findViewById(R.id.altitude)).getText().toString().length()!=0){
double latitude = Double.parseDouble(((EditText)findViewById(R.id.latitude)).getText().toString());
double longitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.longitude)).getText().toString());
double altitude = Double.parseDouble(((AutoCompleteTextView)findViewById(R.id.altitude)).getText().toString());}

关于java - 无法执行 android :onClick - Parsing String to Double 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39854863/

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