0

I am getting the following fatal error with code working perfectly on Android 4.1 and 4.3:

11-10 20:53:21.425    8940-8957/ganbi_collector.com.ganbi_collector E/NativeCrypto﹕ ssl=0x5ca3abe0 cert_verify_callback x509_store_ctx=0x5ee64ab0 arg=0x0
11-10 20:53:21.425    8940-8957/ganbi_collector.com.ganbi_collector E/NativeCrypto﹕ ssl=0x5ca3abe0 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
11-10 20:53:21.502    8940-8940/ganbi_collector.com.ganbi_collector E/linker﹕ load_library(linker.cpp:759): library "libmaliinstr.so" not found
11-10 20:53:21.971    8940-8940/ganbi_collector.com.ganbi_collector E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-10 20:53:21.972    8940-8940/ganbi_collector.com.ganbi_collector E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-10 20:53:22.034    8940-8940/ganbi_collector.com.ganbi_collector E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at ganbi_collector.com.ganbi_collector.MainActivity.setUpMapIfNeeded(MainActivity.java:125)
        at ganbi_collector.com.ganbi_collector.MainActivity.onLocationChanged(MainActivity.java:311)
        at com.google.android.gms.internal.lx$a.handleMessage(Unknown Source)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5434)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

Here is the XML of the target activity

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fragment_container">

    <fragment
        android:id="@+id/ganbi_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</FrameLayout>

And here is the methods caused the error:

    private GoogleMap gMap;

    private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (gMap == null) {
        gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.ganbi_map)).getMap();
        // Check if we were successful in obtaining the map.
        if (gMap != null) {
            // The Map is verified. It is now safe to manipulate the map.
            // Load map preferences
            Log.d("Info", "Map ready for initialization");
            gMap.setMyLocationEnabled(true);
            LogMe(Boolean.toString(locationTrack));
        }
    }
}

    public void onLocationChanged(Location loc) {
    //        Marks marks = new Marks();
    String msg = "Location change received: " +
            Double.toString(loc.getLatitude()) + "," +
            Double.toString(loc.getLongitude());
    LogMe(msg);

    lastLoc = new LatLng(loc.getLatitude(),loc.getLongitude());

    // Move the camera instantly to current location with a zoom of 15.
    if (gMap == null){
        setUpMapIfNeeded();
    } else {
        gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLoc, 15));
    //        marks.getNearMarks(lastLoc);
        getNearMarks(lastLoc);
    }

}

I am debugging using Alcatel pop9 with andorid 4.2.2, the code was working perfeclty on HTC desire 500 with android 4.1 and the same code is workin with Alcatel Idol x2 with no problems.

Ahmed Roshdy
  • 381
  • 6
  • 15
  • What is line 125 in setupMapIfNeeded? Is it `gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.ganbi_map)).getMap();` cause if so I am going to assume findFragmentById() is returning null. – zgc7009 Nov 10 '14 at 19:11
  • Yes it is the line you mentioned, but what might cause this with a version and causing the same error with another version?! – Ahmed Roshdy Nov 10 '14 at 19:22
  • To debug I added the following line to code: Fragment x = getFragmentManager().findFragmentById(R.id.ganbi_map); It does not generate any error and now the error moved to line 127 which the same line you mentioned above. SO obviously the error is generated getMap() not by findFragmentById() – Ahmed Roshdy Nov 10 '14 at 19:28
  • 2
    You are trying to get your map before it is actually done inflating. Try moving that code to onResume instead of onCreate so you know your view has been properly inflated. It was working before, but you were getting kind of lucky with it as it was a race case. – zgc7009 Nov 10 '14 at 19:30
  • Just commenting on the debug code you put in, it can find the fragment because the fragment is in fact there, but the map that it is inflating isn't yet so getMap() gives you the error. – zgc7009 Nov 10 '14 at 19:32

1 Answers1

0

@zgc7009 Gave me the key to the solution in his magical word: it was a race case describing the trial of getting map before inflation. I moved the trial of getting map away from this race and only making this call happens safe later.

I have different explanation on why it worked with other devices: actually this is one of the major challenges in Android development where different hardware with different response times. It seems that the Alcatel pop9 has much better GPS connection time. I was initializing the map get on GPS connection which was ok on other testing mobile phones where the GPS gives enough time for inflater to work.

Ahmed Roshdy
  • 381
  • 6
  • 15