【发布时间】:2014-12-23 04:45:29
【问题描述】:
最近我将一个项目从 Eclipse 迁移到 Android Studio。除了我的一个使用 SupportMapFragment 的片段外,一切都设置好了并且工作正常。下面的 findFragmentById(在 Eclipse 中构建时有效)现在返回 null :(
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SupportMapFragment m = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.safety_map));
xml的sn-p...
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/safety_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="40dp"
map:cameraTargetLat="@string/livesafe_latitude"
map:cameraTargetLng="@string/livesafe_longitude"
map:uiZoomControls="false"
class="com.google.android.gms.maps.SupportMapFragment"/>
这是我在 build.gradle 中的依赖项:
dependencies {
//google analytics
compile 'com.google.apis:google-api-services-analytics:v3-rev103-1.19.0'
//support library for api 10
compile 'com.android.support:support-v4:21.0.0'
//google play services
compile 'com.google.android.gms:play-services:6.1.11'
compile project(':facebook')
compile files('libs/android-support-multidex.jar')
compile files('libs/aws-android-sdk-1.6.0-debug.jar')
compile files('libs/FlurryAnalytics_3.3.2.jar')
}
我没有更改之前在 Eclipse 中工作的 xml 文件或 Fragment 类中的任何代码。
【问题讨论】:
-
什么是lsActivity?你可以试试 getActivity() 吗?
-
尝试使用 getActivity() 但没有运气.... lsActivity 仅用于 bc 我所有的活动都扩展了一个 Common ,这样我就不必到处打字了。 lsActivity = (MyActivity) getActivity();编辑我的问题以避免给其他人造成混淆
-
先生,请执行此操作,只是一个回调测试.. 复制该代码并将其放入片段生命周期的 onresume() 中.. 如果它不返回 null,那么它将是一个回调 lil问题,如果确实发生了,请直接从您的 oncreatview 方法中调用它并从膨胀的布局中获取 id..直接..希望它对您有所帮助..
-
OK 进入 onResume() 也会导致 null 。您能否更具体地使用“直接从膨胀布局中获取 id”?
标签: android eclipse gradle android-studio