我试图获得对创建java的GoogleMap对象的引用,但我不知道如何获得这个引用。
LinearLayout mapLayout = new LinearLayout(customToursActivity);
mapLayout.setId(generateViewId(rootView));
int mapLayoutHeight = Math.round(getResources().getDimension(R.dimen.custom_tour_list_map_size));
mapLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mapLayoutHeight));
layout.addView(mapLayout);
FragmentManager fm = getChildFragmentManager();
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(mapLayout.getId(), supportMapFragment).commit();
GoogleMap mMap = supportMapFragment.getMap();
mMap.setMyLocationEnabled(true);
01-23 17:09:54.168 3795-3795/abff.googlemapsappteste E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: abff.googlemapsappteste, PID: 3795 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' on a null object reference at abff.googlemapsappteste.CustomToursList.createPlacemarksList(CustomToursList.java:458) at abff.googlemapsappteste.CustomToursList.onCreateView(CustomToursList.java:73) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) at android.support.v4.view.ViewPager.populate(ViewPager.java:1105) at android.support.v4.view.ViewPager.populate(ViewPager.java:951) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473) at android.view.View.measure(View.java:17637) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124) at android.view.View.measure(View.java:17637) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444) at android.view.View.measure(View.java:17637) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at android.view.View.measure(View.java:17637) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) at android.view.View.measure(View.java:17637) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536) at android.widget.FrameLayout.onMeasure(FrameLayout.java:436) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618) at android.view.View.measure(View.java:17637) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
有几种方法可以做到这一点。
1).如果您的目标是API级别12之后的应用程序,那么您可以简单地使用MapFragment
而不是SupportMapFragment
。下面是如何将MapFragment
添加到fragment
并获取对GoogleMap
对象的引用的示例代码。
Map mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(mapLayout.getId(), mapFragment).commit();
然后使用getMapAsync()
设置片段的回调。
mapFragment.getMapAsync(this); // your fragment need to implement `OnMapReadyCallback`
@Override
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
}
你可以在这里查阅官方文件
2)但是,如果您的目标应用程序早于API级别12,或者出于某种原因仍然希望使用SupportMapFragment
,那么您还可以使用两个Otpions。
a)。由于映射初始化是异步的,所以您必须等待supportMapFragment.oncreateView()
被调用,这基本上是在您的片段onActivityCreated()
之后,在onActivityCreated()
之前。因此,如果您将supportMapFragment
添加到onActivityCreated()
内部的事务中,那么您将在onStart()
或
public class MyFragment extends Fragment {
private SupportMapFragment mSupportMapFragment;
private GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_with_map, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mSupportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(mapLayout.getId(),mSupportMapFragment).commit();
}
@Override
public void onResume() {
super.onResume();
if (mGoogleMap == null) {
mGoogleMap = mSupportMapFragment.getMap();
}
}
}
如果您面临任何IllegalStateException
(Google bug中跟踪的问题),只需override
您的片段ondetach()
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
您可以重写
SupportMapFragment
的onActivityCreated()
。
public class MyFragment extends Fragment {
private SupportMapFragment mSupportMapFragment;
private GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_with_map, container, false);
mSupportMapFragment = new SupportMapFragment() {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mGoogleMap = mSupportMapFragment.getMap();
if (mGoogleMap != null) {
mGoogleMap.setMyLocationEnabled(true);
}
}
};
getChildFragmentManager().beginTransaction().replace(mapLayout.getId(), mSupportMapFragment).commit();;
return view;
}
}
test1 a=new test 1(3,“b”);
问题内容: 我有一个具有@Embedded类Profile的User类。如何给Profile实例引用其所有者User类? 问题答案: 假设JPA而不是严格地处于hibernate状态,则可以通过应用getter / setter对而不是私有成员本身来实现。 但是,我想问的是,在这种情况下,嵌入式实体是否就是您想要的,而不是@OneToOne关系,或者只是将Profile类“展平”到User中。@E
我正在尝试使用Dozer从JAXB实体“JaxbParent”填充Hibernate实体“Parent”。我的Hibernate实体: 我的Jaxb实体看起来像: My Dozer xml映射配置: 因此,为了将孩子列表转换为孩子集,我使用CustomConverter,并且我得到了正确的数据字段。问题是,Hibernate需要每个孩子都有对父对象的引用(以执行保存),但目前它为空。我试图将“th
问题内容: 指针是仅用于实现Java参考变量还是实际上如何实现?以下是Java语言规范中的代码行 4.3.1对象对象是类实例或数组。引用值(通常只是引用)是指向这些对象的指针,还有一个特殊的空引用,它不引用任何对象。 这是否意味着一直都是指针? 问题答案: 在现代JVM中,引用被实现为地址。 回到HotSpot的第一个版本(对于“经典VM”来说更早一些),引用被实现为句柄。那是指向指针的固定指针。
问题内容: 我正在尝试编写代码来帮助我创建XML对象。例如,我将字符串作为函数的输入,它将返回XMLObject。 当我在网上搜索时,通常会看到有关创建XML文档的示例。因此,我看到的有关创建XML并写入文件并创建文件的所有内容。但是我做了类似的事情: 您认为创建XML对象就足够了吗?还可以请您帮我如何从XML中获取数据吗?例如,如何从XML获取数据? 非常感谢大家 编辑1: 实际上,我现在想拥有
问题内容: 在代码中: 现在,将是7,x将是8。但是实际上,我想改变。我可以分配引用吗? 例如,在C ++中,可以达到以下目的: 现在,这两个&将是9 问题答案: 你不能。正如其他答案指出的那样,您可以(ab?)使用可变对象的别名来达到类似的效果。但是,这与C ++参考不是同一回事,我想解释一下为避免误解而实际发生的情况。 您会看到,在C ++(和其他语言)中,变量(以及对象字段以及集合中的条目等