按钮无法找到onMapSearch函数

时间:2017-02-24 21:12:13

标签: java android

我有一些代码可以在地图中进行搜索(至少应该这样,但是当你点击按钮时应用程序会崩溃)。这是一个Android应用程序。

按钮的xml和搜索栏位于:

 <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="Search Location Here" />
        <Button
            android:id="@+id/search_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:onClick="onMapSearch"
            android:text="Search" />

然后按钮功能的功能在这里:

public void onMapSearch(View view) {
    EditText locationSearch = (EditText) findViewById(R.id.editText);
    String location = locationSearch.getText().toString();
    Geocoder geocoder = new Geocoder(this);
    List<android.location.Address> addressList = null;

    try {
        addressList = geocoder.getFromLocationName(location, 1);

    } catch (IOException e) {
        e.printStackTrace();
    }
    android.location.Address address = addressList.get(0);
    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}

Logcat说按钮找不到onMapSearch的功能,有什么建议吗? Logcat在这里:

 E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: pkc.trafficquest.sccapstone.trafficquest, PID: 3488
                  java.lang.IllegalStateException: Could not find method MapsActivity.onMapSearch(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'search_button'
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/pkc.trafficquest.sccapstone.trafficquest-1/base.apk"],nativeLibraryDirectories=[/data/app/pkc.trafficquest.sccapstone.trafficquest-1/lib/x86, /system/lib, /vendor/lib]]

1 个答案:

答案 0 :(得分:0)

onClick在“活动中不在片段中”查找方法,如果在片段中声明方法,则应使用button.setOnClickListener

如果您希望您的代码更好&#34;你可以看一下ButterKnife