我正在尝试在导航抽屉(使用Android Studio)中制作一个简单的地图应用程序,但我面临一个奇怪的情况。我在虚拟设备API 24(也是真正的Marshmallow设备)中尝试过,它工作正常(日志猫中没有错误信息)。
但是当我尝试使用Virtual Device API 17(也就是真正的Kitkat 4.4.2设备)时,应用程序会抛出错误。
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'
}
在虚拟设备API 17中进行测试时的logcat
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)
我已经尝试了解决方案:
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
但它不起作用。它在我的真实Marshmallow设备和虚拟设备API 24中工作正常,并且错误发生在我真正的kitkat(4.4.2)设备和虚拟设备API 17中。
答案 0 :(得分:0)
如果您在代码中使用涟漪效应。
添加: -
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)
}
答案 1 :(得分:0)
我找到了答案。我使用MapView而不是supportMapFragment或MapFragment。
这是链接:Android Drawer Example, how to implement Google Map Fragment