我已经在SO上阅读了无数NullPointerException
个关于Google Maps v2 api for android的帖子,似乎没有人回答这个问题。
我一直为NullPointerException
获取SupportMapFragment
。我已经尝试过几乎所有的东西,而且此时我的智慧已经结束了。我的应用程序每次尝试启动时都会崩溃,如果没有为应用程序实现googlemap,它的工作正常。
我已查看是否缺少Google Play服务,不是。它得到了谷歌播放的服务和包名称。我检查了我的清单,所有设置都正确(显然我没有向你们展示我的Api键)。那是什么?
是因为我正在扩展ActionBarActivity而不是FragmentActivity吗?根据我的理解,这不应该导致问题,因为ActionBarActivity是FragmentActivity的扩展。
那怎么了?我不明白。我已经尝试了一切。
SupportMapFragment class
此课程中出现问题
public class GoogleMapFragment extends SupportMapFragment {
private final String TAG = "GoogleMapFragment";
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Test to see if GooglePlayServices is available
*/
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == 9) {
Log.d(TAG, "Service Invalid");
}
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) == 3) {
Log.d(TAG, "Service Disabled");
}
// Get a handle to the Map Fragment
setupMapIfNeeded();
}
@Override
public void onResume() {
super.onResume();
setupMapIfNeeded();
}
private void setupMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapView))
.getMap(); // SAYS PROBLEM IS HERE
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
MainActivity
public class MainActivity extends ActionBarActivity {
//Log TAG
private final String TAG = "MainActivity";
private final static String MAP_TAB = "Map";
};
/*
* Setup fragments
*/
private Fragment mapFragment;
/*
* onCreate method which will create the action bar and the tabs undeneath
* action bar
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Setup Fragments
*/
mapFragment = new GoogleMapFragment();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/*
* Setup tabs underneath actionbar
*/
ActionBar bar = getSupportActionBar();
bar.setTitle(R.string.app_name);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/*
* declare tabs
*/
Tab mapTab = bar.newTab();
/*
* Set titles
*/
mapTab.setText(MAP_TAB);
/*
* Set Icons for tabs [ADD IN LATER]
*/
//mapTab.setIcon(icon);
/*
* Setup tab listeners
*/
mapTab.setTabListener(new MyTabListener<GoogleMapFragment>(this,
MAP_TAB, GoogleMapFragment.class));
/*
* add to bar
*/
bar.addTab(mapTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
...
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
/*
* When app is on pause retain activity
*/
@Override
public void onPause() {
super.onPause();
}
...
...
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="com.curio.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Prohibit app only to android devices that have cameras and OpenGL ES v.2 -->
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.curio.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AI......................" />
</application>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_map.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
LogCat
06-07 10:28:49.659: E/AndroidRuntime(1944): FATAL EXCEPTION: main
06-07 10:28:49.659: E/AndroidRuntime(1944): Process: com.app, PID: 1944
06-07 10:28:49.659: E/AndroidRuntime(1944): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.MainActivity}: java.lang.NullPointerException
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.os.Handler.dispatchMessage(Handler.java:102)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.os.Looper.loop(Looper.java:136)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-07 10:28:49.659: E/AndroidRuntime(1944): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 10:28:49.659: E/AndroidRuntime(1944): at java.lang.reflect.Method.invoke(Method.java:515)
06-07 10:28:49.659: E/AndroidRuntime(1944): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-07 10:28:49.659: E/AndroidRuntime(1944): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-07 10:28:49.659: E/AndroidRuntime(1944): at dalvik.system.NativeStart.main(Native Method)
06-07 10:28:49.659: E/AndroidRuntime(1944): Caused by: java.lang.NullPointerException
06-07 10:28:49.659: E/AndroidRuntime(1944): at com.curio.GoogleMapFragment.setupMapIfNeeded(GoogleMapFragment.java:72)
06-07 10:28:49.659: E/AndroidRuntime(1944): at com.curio.GoogleMapFragment.onCreate(GoogleMapFragment.java:42)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.Activity.performStart(Activity.java:5241)
06-07 10:28:49.659: E/AndroidRuntime(1944): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
答案 0 :(得分:0)
这解决了我的问题:Android SupportMapFragment can't see map inside a fragment
我忘了打电话给super.onCreateView()
。