gpt4 book ai didi

java - 无法解析 getSupportFragmentManager()

转载 作者:行者123 更新时间:2023-11-30 01:41:32 34 4
gpt4 key购买 nike

我在 fragment 内使用 map fragment 。但是在 getSupportFragmentManager() 上出现错误,即无法解析 getSupportFragmentManager()

我尝试使用 geChildFragmentManager() 所以它工作正常。但它在 api 19 上崩溃,它需要 api 21 及更高版本。我想为我的应用程序获取最低的 API。

我该怎么做?

我也尝试使用支持库。还是报错..

谁能帮忙...?

fragment

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)

最佳答案

改变:

 getSupportFragmentManager()

到:

 getActivity().getSupportFragmentManager()

您正在使用 android.support.v4.app.Fragment 因此还要确保您的 Activity 扩展 FragmentActivity .

关于java - 无法解析 getSupportFragmentManager(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439697/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com