【发布时间】:2016-03-30 03:01:03
【问题描述】:
我在片段内使用地图片段。但是在getSupportFragmentManager() 上出现错误,即无法解析getSupportFragmentManager()。
我尝试使用geChildFragmentManager(),所以它工作正常。但它在需要 api 21 及更高版本的 api 19 上崩溃。我想为我的应用获取最低的 api。
我该怎么做??
我也尝试使用支持库。还是报错..
有人可以帮忙吗..?
片段
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
public class GoFoodNearMeFragment extends Fragment {
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
boolean mUpdatesRequested = false;
private GoogleMap mGoogleMap;
private Toolbar toolbar;
private Intent i;
private GPSTracker gps;
private LatLng curentpoint;
double latitude;
double longitude;
private RelativeLayout suggest;
private RelativeLayout search;
public GoFoodNearMeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_go_food_near_me, container, false);
mGoogleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setCompassEnabled(true);
mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
gps = new GPSTracker(getActivity());
gps.canGetLocation();
latitude = gps.getLatitude();
longitude = gps.getLongitude();
curentpoint = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(curentpoint).zoom(19f).tilt(70).build();
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
suggest= (RelativeLayout)v.findViewById(R.id.suggest);
search =(RelativeLayout)v.findViewById(R.id.LinearSearch);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(),SearchRestaurantsActivity.class);
startActivity(i);
}
});
suggest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i=new Intent(getActivity(),SuggestRestaurantActivity.class);
startActivity(i);
}
});
return v;
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="@android:color/white"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="65dp"
android:id="@+id/LinearSearch"
android:layout_below="@+id/toolbar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/search"
android:id="@+id/textView12"
android:layout_gravity="center"
android:layout_centerInParent="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView29"
android:layout_gravity="center"
android:background="@drawable/ic_search_black_24dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp" />
</RelativeLayout>
<fragment android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/map"
tools:context=".GoFoodNearMeFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_below="@+id/LinearSearch"
android:layout_alignParentEnd="true"
android:layout_marginTop="05dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@color/black_trans80"
android:layout_gravity="bottom"
android:id="@+id/suggest"
android:layout_alignParentBottom="true">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/imageView35"
android:layout_gravity="center"
android:background="@drawable/lightbulb14"
android:layout_alignParentTop="false"
android:layout_toStartOf="@+id/textView13"
android:layout_alignParentBottom="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/suggestRest"
android:id="@+id/textView13"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:layout_marginLeft="05dp" />
</RelativeLayout>
</RelativeLayout>
dependencies
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
applicationId "com.example.siddhi.go_jek"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
依赖
{
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'com.google.android.gms:play-services-identity:8.3.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'info.hoang8f:android-segmented:1.0.6'
compile 'com.android.support:support-v4:23.1.0'
}
得到这个异常
FATAL EXCEPTION: main
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: Process: com.example.siddhi.go_jek, PID: 10525
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: java.lang.NullPointerException
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: at com.example.siddhi.go_jek.GoFoodNearMeFragment.onCreateView(GoFoodNearMeFragment.java:53)
12-23 11:38:14.363 10525-10525/com.example.siddhi.go_jek E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
【问题讨论】:
-
使用
getActivity(). getSupportFragmentManager(),因为方法可用FragmentActivity而不是Fragment类,或者您也可以使用getChildFragmentManager(),您可以在不使用getActivity()的情况下访问它,因为此方法在片段类中可用.谢谢 -
它与 getChildFragmentManager 一起工作,但它在 api 19 以上工作我也想在 19 api 以下工作我的应用程序。尝试使用 getActivity 仍然会因空指针异常而崩溃...@ρяσѕρєя K
-
使用
getChildFragmentManager()是嵌套 SupporotMapFragment 的正确方法。看看这个答案中的工作代码(第一个例子):stackoverflow.com/a/32579020/4409409 -
但它在 api 21 下工作.. @Daniel Nugent
-
api-17 及更高版本支持
getChildFragmentManager()的原生片段。如果您使用支持库片段,那么它也可以在较低的 api 级别上工作。见这里,对于原生片段,在 api 级别 17 中添加:developer.android.com/reference/android/app/…
标签: java android android-fragments supportmapfragment