我在使用viewpager的tab片段中使用了map,但是Map在我的应用中显示了Null指针异常。
我添加了权限<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
和API密钥正确
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
我的代码是:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class HomeTabFragment2 extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public HomeTabFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.home_tab_fragment_1, container, false);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(9.2700, 76.7800);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Pathanamthitta"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
我的Logcat:
1-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: FATAL EXCEPTION: main
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: Process: com.example.rubin, PID: 5673
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at com.example.rubin.HomeTabFragment2.onCreateView(HomeTabFragment2.java:34)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1078)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:940)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.View.measure(View.java:17547)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
11-01 23:58:24.139 5673-5673/com.example.rubin E/AndroidRuntime: at android.view.Choreogra
当我将碎片更改为碎片活动时,它的工作非常顺利。 但我无法弄清楚它将如何在片段中工作。请帮我。
答案 0 :(得分:4)
您的logcat引发
AndroidRuntime:java.lang.NullPointerException:尝试调用 虚方法&#39;无效 com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)&#39; 在空对象引用上
您可以尝试使用getSupportFragmentManager
或getChildFragmentManager
SupportMapFragment m = ((SupportMapFragment)
.getSupportFragmentManager().findFragmentById(R.id.map));
或 (完美解决方案)
SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
public final FragmentManager getChildFragmentManager()
在API级别17中添加返回私有FragmentManager以进行放置和 管理此片段内的片段。
有关详细信息,请查看
findFragmentById for SupportMapFragment returns null in Android Studio
答案 1 :(得分:1)
尝试改变以下行:
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
TO:
SupportMapFragment mapFragment = (SupportMapFragment)v.getFragmentManager()
.findFragmentById(R.id.map);