I get a nullpointer everytime I try to instance the map.
I found a lot of articles about it but that didn't work. The map needs to exist in another fragment.
This is my code:
public class ContactFragment extends Fragment {
private Contact contact = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contact = Shared.dbRepo.contactsRepository.getFirst();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_price, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.title(contact.getTitle());
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.position(getLocation(contact.getAddress()));
map.addMarker(markerOptions);
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
MainActivity mainActivity = (MainActivity) activity;
mainActivity.showActionBar();
}
private LatLng getLocation(String strAddress) {
Geocoder coder = new Geocoder(getActivity().getApplicationContext());
List<Address> address;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
Address location = address.get(0);
return new LatLng(location.getLatitude(), location.getLongitude());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_margin="5dp">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/contact_address" />
<TextView
android:id="@+id/tv_contact_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/text_gray"
android:autoLink="map"
android:text="@string/opening_hours" />
</TableRow>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:autoLink="email"
android:text="@string/contact_email" />
<TextView
android:id="@+id/tv_contact_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/text_gray"
android:text="@string/opening_hours" />
</TableRow>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/contact_telephone" />
<TextView
android:id="@+id/tv_contact_telephone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/text_gray"
android:autoLink="phone"
android:text="@string/opening_hours" />
</TableRow>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/contact_website" />
<TextView
android:id="@+id/tv_contact_website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="@color/text_gray"
android:autoLink="web"
android:text="@string/opening_hours" />
</TableRow>
</TableLayout>
</LinearLayout>
Edit: This is the exception I get:
05-20 16:32:18.295 6663-6663/be.appmax.twentebad.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: be.appmax.twentebad.app, PID: 6663
java.lang.NullPointerException
at be.appmax.twentebad.app.fragments.ContactFragment.onViewCreated(ContactFragment.java:48)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:952)
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:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5144)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
This is line 48:
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();