0

I am trying to make a simple map application in navigation drawer (using Android Studio) but I am facing a weird case. I tried in Virtual Device API 24 (also real Marshmallow device) and it works fine (no error message in log cat).

But when I tried in Virtual Device API 17 (also real Kitkat 4.4.2 device), the application throws an error.

LocationFragment.java

package dev.dtgultom.ontelbike.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import dev.dtgultom.ontelbike.R;

public class LocationFragment extends Fragment implements OnMapReadyCallback {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_location,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng stockholm = new LatLng(59.3293, 18.0686);
        googleMap.addMarker(new MarkerOptions().position(stockholm).title("User Position"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stockholm, 15));
    }
}

fragment_location.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"
        tools:context="dev.dtgultom.ontelbike.Fragment.LocationFragment"/>

</FrameLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dev.dtgultom.ontelbike">

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" />
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBar"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/api_key" />
    </application>

</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "dev.dtgultom.ontelbike"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.android.support:support-vector-drawable:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.google.android.gms:play-services-maps:11.0.4'
    compile 'com.google.android.gms:play-services-location:11.0.4'
    testCompile 'junit:junit:4.12'
}

logcat when testing in virtual device API 17

09-01 13:38:27.327 23397-23397/dev.dtgultom.ontelbike E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-01 13:39:49.043 23397-23397/dev.dtgultom.ontelbike W/dalvikvm: VFY: unable to resolve check-cast 34 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/internal/zzbgz;
09-01 13:39:49.043 23397-23397/dev.dtgultom.ontelbike D/dalvikvm: VFY: replacing opcode 0x1f at 0x0010
09-01 13:39:49.047 23397-23397/dev.dtgultom.ontelbike I/zzbx: Making Creator dynamically
09-01 13:39:49.191 23397-23397/dev.dtgultom.ontelbike E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        java.lang.NullPointerException
                                                                            at dev.dtgultom.ontelbike.Fragment.LocationFragment.onViewCreated(LocationFragment.java:29)
                                                                            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:899)
                                                                            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
                                                                            at android.app.BackStackRecord.run(BackStackRecord.java:682)
                                                                            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
                                                                            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
                                                                            at android.os.Handler.handleCallback(Handler.java:725)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                            at android.os.Looper.loop(Looper.java:137)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:511)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                            at dalvik.system.NativeStart.main(Native Method)

I already tried the solution in:

https://stackoverflow.com/questions/29655172/google-maps-api-v2-android-error
https://stackoverflow.com/questions/30991087/mapfragment-getmapasyncthis-nullpointerexception
https://stackoverflow.com/questions/28109206/null-pointer-exception-at-mapfragment-getmapasyncthis-while-initializing-googl
https://stackoverflow.com/questions/30991087/mapfragment-getmapasyncthis-nullpointerexception
https://stackoverflow.com/questions/26340398/java-lang-nullpointerexception-with-android-fragment

But it is not working. It works fine in my real Marshmallow device and virtual device API 24 and the error occur in my real kitkat (4.4.2) device and virtual device API 17.

halfer
  • 19,824
  • 17
  • 99
  • 186
Theo
  • 33
  • 6
  • are you using ripple effect in your drawable? – Namrata Sep 01 '17 at 12:10
  • I don't think so. Because I use the template from Android Studio when creating the Navigation Drawer. I only add some code like FragmentManager fm = getFragmentManager(); to navigate between fragments, example : fm.beginTransaction().replace(R.id.content_frame, new LocationFragment()).commit(); – Theo Sep 01 '17 at 12:16
  • Hi Theo. You last edit removed good changes made by @dda - please be careful not to overwrite other people's work. When you are in the edit window, a notification will be displayed to you if the post is edited - if this happens, copy your current state to the clipboard, then back out of the edit, refresh and try again. – halfer Sep 01 '17 at 12:23
  • @halfer, sorry I didn't notice – Theo Sep 01 '17 at 20:59
  • No worries, it's all good. In case you do not know, [the revision history](https://stackoverflow.com/posts/46000004/revisions) for any question can be found by clicking the edit time in the bottom of your post (to the left of your own avatar). – halfer Sep 01 '17 at 21:17

2 Answers2

0

If you using ripple effect in your code.

Add this :-

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
       //and ripple button (Call some material design APIs here)
    }
 else {
    //add simple button / image (Implement without material design)
    }
Namrata
  • 1,683
  • 1
  • 17
  • 28
0

I found the answer. I use MapView instead of supportMapFragment or MapFragment.

This is the link: Android Drawer Example, how to implement Google Map Fragment

Theo
  • 33
  • 6