无法接受回收器视图上的浮动输入

问题描述 投票:0回答:1

我正在开发一个评论申请,在提交评论时,评论者必须包括餐厅的坐标,然后填写地图。

但我正在努力如何接受浮点输入,字符串输入工作正常。我在下面的代码中突出显示我遇到困难,我尝试使用“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" />
java android android-recyclerview
1个回答
2
投票
String latitude = mLat.getText().toString();
float f = Float.parseFloat(latitude);

你必须在字符串中输入,然后转换它。

© www.soinside.com 2019 - 2024. All rights reserved.