After trying this for about 3 days and digging through a few threads with no luck I have to send sos to anybody who can kindly look into my code. The app functions flawlessly until I flip to landscape mode.
I'm implementing the solutions as describe in the threads below, so I move the code around in many ways but I've got errors like: Nullpointer exception. View with id xxxx not found. Child already has a parent.
No matter what I do, it keeps crashing. See please the threads I've followed but without luck
thread 1, thread 2, thread 3, thread 4, thread 5
To my disappointment I downloaded this nice app (Airbnb google map) to see how they solved this problem but after I flip the phone it crashes exactly like mine. I'll probably send a note to the developer.
I read in the threads opinions are different and concepts given are interesting but I can't fix this. Would appreciate some new lines of code.
The code
public class MapsActivity extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String LOG_TAG = MapsActivity.class.getSimpleName();
private static final int CURSOR_LOADER_ID = 0;
private Cursor initQueryCursor;
private GetSet coord;
private AppPreferences sPref;
private LatLng home;
private SupportMapFragment sMap;
private FragmentManager fm;
private GoogleMap mMap;
private static final String FRAGMENT_LISTS =
"net.simonvt.schematic.samples.ui.SampleActivity.LISTS";
public MapsActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sPref = new AppPreferences(getActivity());
SyncAdapter.syncImmediately(getActivity(),
sPref.getCoordBody("lat"),
sPref.getCoordBody("lon"));
getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this);
// getLoaderManager().restartLoader(CURSOR_LOADER_ID, null, this);
// mMapCursor = new MapCursor(getActivity(), null, 0);
// ConnectivityManager cm =
// (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
// NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
// getLoaderManager().restartLoader(CURSOR_LOADER_ID, null, this);
}
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(savedInstanceState==null) {
View v = inflater.inflate(R.layout.yelp_google_map, container, false);
fm = getChildFragmentManager();
sMap = (SupportMapFragment) fm.findFragmentById(R.id.map);
mMap = sMap.getMap();
initQueryCursor = getActivity().getContentResolver().query(QuoteProvider.Quotes.CONTENT_URI,
new String[]{QuoteColumns.URL, QuoteColumns.DISPLAY_PHONE, QuoteColumns.RATING_IMG,
QuoteColumns.ID_BUSINESS_NAME, QuoteColumns.DISPLAY_ADDRESS, QuoteColumns.POSTAL_CODE,
QuoteColumns.LATITUDE, QuoteColumns.LOGITUDE}, null,
null, null);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
if (initQueryCursor != null) {
DatabaseUtils.dumpCursor(initQueryCursor);
initQueryCursor.moveToFirst();
for (int i = 0; i < initQueryCursor.getCount(); i++) {
home = new LatLng(Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("latitude"))),
Double.valueOf(initQueryCursor.getString(initQueryCursor.getColumnIndex("longitude"))));
Marker m = mMap.addMarker(new MarkerOptions().position(home).
title(initQueryCursor.getString(initQueryCursor.getColumnIndex("id_bussines_name"))));
initQueryCursor.moveToNext();
builder.include(home);
}
if (home != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 215, 210, 0));
}
initQueryCursor.close();
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(getContext(), DetailActivity.class);
intent.putExtra("title", marker.getTitle());
startActivity(intent);
}
});
}
return v;
}
return null;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), QuoteProvider.Quotes.CONTENT_URI,
new String[]{ QuoteColumns._ID, QuoteColumns.DISPLAY_ADDRESS, QuoteColumns.DISPLAY_PHONE,
QuoteColumns.RATING, QuoteColumns.URL, QuoteColumns.POSTAL_CODE, QuoteColumns.ID_BUSINESS_NAME,
QuoteColumns.LATITUDE,
QuoteColumns.LOGITUDE},
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (loader != null) {
// mMapCursor.swapCursor(data);
initQueryCursor = data;
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
the layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mem.edu.joshua.MapsActivity"
android:background="#009688">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
This is only one of the errors I get. Depending on what I change I get a different error like what I mentioned above
FATAL EXCEPTION: main
Process: mem.edu.joshua, PID: 21406
java.lang.IllegalArgumentException: No view found for id 0x7f0c0081 (mem.edu.joshua:id/map) for fragment MapsActivity{425ff5b8 #2 id=0x7f0c0081 MapsActivity}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
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:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)