我正在尝试绑定一个放在MapFragment中的按钮 我希望我的地图有一个按钮,就像一个菜单按钮
这是我的片段
public class MapFragment extends Fragment implements
OnMapReadyCallback,
View.OnClickListener{
public SupportMapFragment mapFragment;
private GoogleMap mMap;
FragmentMapBinding fragmentMapBinding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fragmentMapBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_map,container,false);
View rootView = fragmentMapBinding.getRoot();
fragmentMapBinding.setHandler(this);
mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return rootView;
}
}
这是片段的.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="handler"
type="com.plotalong.android.activity.fragment.MapFragment"/>
</data>
<fragment xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.plotalong.android.activity.MapsActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:onClick="@{() -> handler.menuButtonClick()}"
/>
</fragment>
这是我得到的错误日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.plotalong.android/com.plotalong.android.activity.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1092)
at android.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1149)
at android.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:600)
at com.plotalong.android.databinding.FragmentMapBinding.<init>(FragmentMapBinding.java:24)
at com.plotalong.android.databinding.FragmentMapBinding.bind(FragmentMapBinding.java:134)
at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:15)
at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:185)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:120)
at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:88)
at com.plotalong.android.activity.fragment.MapFragment.onCreateView(MapFragment.java:66)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5274)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
代码有什么问题?
答案 0 :(得分:0)
问题是您无法在<Button>
标记内粘贴<View>
(或任何其他<fragment>
)。一般情况下,您不应该尝试修改SupportMapFragment
内部的SupportMapFragment
内部的内容。不过,您可以在Button
之上或之下放置SupportMapFragment
。 (在SupportMapFragment
之外)