此活动具有嵌套片段,其中一个嵌套片段为SupportMapFragment。如果没有安装Google Play服务,则会将其替换为另一个拥有ImageView的片段。
当我触摸ImageView 时,异常会抛出,例外情况是:
11-17 21:00:37.477: E/AndroidRuntime(6189): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=market://details?id=com.google.android.gms flg=0x80000 pkg=com.android.vending }
Google Play服务尚未安装,因此网址://'按预期失败。但是:
为什么点击事件仍然在听?此时已删除SupportMapFragment。
相关代码部分:
public class DetailFragment extends Fragment {
///....
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
SupportMapFragment location = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mGoogleMap = location.getMap();
if (mGoogleMap != null) {
// add markers here
} else {
final View map = view.findViewById(R.id.map);
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
map.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = map.getWidth();
int height = map.getHeight();
String url = "http://maps.googleapis.com/maps/api/staticmap?center=" + mItem.lat + "," + mItem.lon + "&zoom=" + (int) mItem.zoom + "&size=" + width + "x" + height
+ "&sensor=false&key=API_KEY_HERE";
Bundle args = new Bundle();
args.putString("url", url);
ImageFragment img = new ImageFragment();
img.setArguments(args);
getChildFragmentManager().beginTransaction().replace(R.id.map, img).commit();
}
});
}
}
...
} // DetailFragmentClass
XML相关
<fragment
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="@dimen/promo_body_map_weight"
class="com.google.android.gms.maps.SupportMapFragment" />
我想我在某些方面错过了一些清理工作。但我无法看清楚。
如何点击点击事件?
正如评论中建议的那样,我也尝试使用XML中的FragmentLayout加载片段onCreateView
,但问题仍然存在。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_promo_datailed,
container, false);
//...
mSupportMapFragment = new SupportMapFragment();
getChildFragmentManager().beginTransaction().add(R.id.map, mSupportMapFragment).commit();
// ...
return rootView;
}
XML
<FrameLayout
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="@dimen/promo_body_map_weight"
/>