我有一个片段,应该与谷歌地图加载一个标记在上半部分,并在屏幕的下半部分的文本视图。当我尝试启动它时,我得到这个错误:
06-26 12:34:06.398 20009-20009/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.beerportfolio.beerportfoliopro, PID: 20009
java.lang.NullPointerException
at com.beerportfolio.beerportfoliopro.LocationPage.onResume(LocationPage.java:89)
at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
我的LocationPage代码是:
public class LocationPage extends Fragment {
GoogleMap mMap;
private SupportMapFragment fragment;
private GoogleMap map;
String lattitude;
String longitude;
String name;
//Mandatory Constructor
public LocationPage() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.locationpage,container, false);
//todo: set title to brewery name
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
name = prefs.getString("name", null);
String hours = prefs.getString("hours", null);
lattitude = prefs.getString("lat", null);
longitude = prefs.getString("lng", null);
String phone = prefs.getString("phone", null);
getActivity().setTitle(name);
//todo: set map
//todo: display rest of information
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (fragment == null) {
fragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, fragment).commit();
}
}
@Override
public void onResume() {
super.onResume();
//todo: add markers
double latD = Double.parseDouble(lattitude);
double lngD = Double.parseDouble(longitude);
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(name));
}
}
LocationPage的第89行是:
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latD, lngD))
.title(name));
我的xml for LocationPage是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
可能您有一个空值的变量(名称、纬度、经度或mMap)。尝试在logcat中打印这些变量。