我正在开发评论应用程序,提交评论时,评论者必须包括餐厅的坐标,然后将其填入地图。
但是我在如何接受浮点输入方面苦苦挣扎,字符串输入工作正常。我在下面的代码中突出显示了我遇到的困难,我尝试使用“ Float.valueOf ”,但是这导致我的应用程序崩溃。
感谢您的帮助。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_new_note, container, false);
mAttractionName = view.findViewById(R.id.Submit_attractionName);
mReview = view.findViewById(R.id.Submit_review);
mAttractionAddress =
view.findViewById(R.id.Submit_attraction_Address);
mCuisine = view.findViewById(R.id.Submit_Cuisine);
mLat = view.findViewById(R.id.Submit_latitude);
mLong = view.findViewById(R.id.Submit_longitude);
mCreate = view.findViewById(R.id.create);
mCancel = view.findViewById(R.id.cancel);
mCancel.setOnClickListener(this);
mCreate.setOnClickListener(this);
getDialog().setTitle("New Attraction");
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.create:{
// insert the new note
String attractionName = mAttractionName.getText().toString();
String review = mReview.getText().toString();
String attractionAddress = enter code
heremAttractionAddress.getText().toString();
String cuisine = mCuisine.getText().toString();
Float latitude = mLat.getText().toString(); (ISSUES HERE)
Float longitude = mLong.getText().toString();
if(!attractionName.equals("")){
mSUBMITACTIVITY.createNewNote(attractionName, re`enter
code here`view, cuisine, attractionAddress, latitude, longitude);
getDialog().dismiss();
}
else{
Toast.makeText(getActivity(), "Enter an Attraction",
Toast.LENGTH_SHORT).show();
}
break;
}
case R.id.cancel:{
getDialog().dismiss();
break;
}
}
}
EditText的XML
<android.support.design.widget.TextInputLayout
android:id="@+id/content_wrapper4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/Submit_latitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/note_latitude"
android:imeOptions="actionNext"
android:inputType="numberDecimal|numberSigned" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/content_wrapper5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/Submit_longitude"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/note_longitude"
android:imeOptions="actionNext"
android:inputType="numberDecimal|numberSigned" />
答案 0 :(得分:2)
String latitude = mLat.getText().toString();
float f = Float.parseFloat(latitude);
您必须以字符串形式输入输入,然后将其转换。