【问题标题】:Add a search button to maps向地图添加搜索按钮
【发布时间】:2015-11-04 15:05:32
【问题描述】:

我正在尝试向 android 地图添加搜索栏。 该地图将作为片段之一添加到项目中。该错误似乎是由this 引入的。任何帮助表示赞赏。提前致谢。

public class Maps extends Fragment {
    //Initial code

   public void onSearch(View view) {
        EditText location_tf = (EditText) getView().findViewById(R.id.TFaddress);
        String location = location_tf.getText().toString();
        List < Address > addressList = null;

        if (location != null || !location.equals(""))
        Geocoder geocoder = new Geocoder(this);

        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Address address = addressList.get(0);
    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText android:layout_width="287dp"
            android:layout_height="wrap_content"
            android:id="@+id/TFaddress"/>
        <Button style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"
            android:text="Search"
            android:id="@+id/button"
            android:layout_gravity="right"
            android:onClick="onSearch" />
        <com.google.android.gms.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

错误:

Error:(83, 46) error: incompatible types: MAPS cannot be converted to Context
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output.    
Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.

【问题讨论】:

    标签: android xml google-maps android-studio google-maps-android-api-2


    【解决方案1】:

    根据the documentationGeocoder 构造函数需要Context,而不是Fragment,所以这一行

    Geocoder geocoder = new Geocoder(this);
    

    应该是

    Geocoder geocoder = new Geocoder(getActivity());
    

    另外,请注意您可能希望在 if 语句中添加括号:

    if (location != null || !location.equals(""))
        Geocoder geocoder = new Geocoder(this);
    
        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • 感谢您的帮助。但是,在纠正我的代码后,当我尝试运行它时,TomCat:java.lang.IllegalStateException:在视图类 android.support 上定义的 android:onClick 属性的父或祖先上下文中找不到方法 onSearch(View)。 v7.widget.AppCompatButton 与 id '按钮'
    • 尝试公开您的onSearch(View view)public onSearch(View view)
    • 我的错误,你的onSearch方法必须在你的Activity,而不是你的Fragmentstackoverflow.com/questions/21192386/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2011-04-25
    • 1970-01-01
    • 2023-01-09
    相关资源
    最近更新 更多