我已经遇到了一段时间这个问题,我的主要活动带有按钮,这些按钮会将用户带到视图中带有google map的另一个活动,但是如果用户点击按钮,应用程序就会崩溃并给出一条消息,提示“不幸的是,(应用名称)已停止。”
这是与谷歌地图的活动:
package location.hive;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdate;
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.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
public class DetailActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int DIALOG_REQUEST = 9001;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_with_map);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync();
String city = getIntent().getStringExtra("city");
setTitle(getString(R.string.landon_hotel) + ", " + city);
Hotel hotel = DataProvider.hotelMap.get(city);
if (hotel == null) {
Toast.makeText(this, getString(R.string.error_find_hotel) + ": "
+ city, Toast.LENGTH_SHORT).show();
return;
}
TextView cityText = (TextView) findViewById(R.id.cityText);
cityText.setText(hotel.getCity());
TextView neighborhoodText = (TextView) findViewById(R.id.neighborhoodText);
neighborhoodText.setText(hotel.getNeighborhood());
TextView descText = (TextView) findViewById(R.id.descriptionText);
descText.setText(hotel.getDescription() + "\n");
if (servicesOK() && initMap()) {
Geocoder gc = new Geocoder(this);
List<Address> list;
try {
list = gc.getFromLocationName(hotel.getAddress(), 1);
Address address = list.get(0);
double lat = address.getLatitude();
double lng = address.getLongitude();
LatLng latLong = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLong, 15);
mMap.moveCamera(update);
MarkerOptions options = new MarkerOptions()
.title(getString(R.string.landon_hotel) + ", " + city)
.position(new LatLng(lat, lng));
mMap.addMarker(options);
} catch (IOException e) {
Toast.makeText(this, getString(R.string.error_finding_hotel), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d(this.getLocalClassName(), e.getMessage());
}
}
}
public boolean servicesOK() {
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(result)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(result, this, DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, getString(R.string.error_connect_to_services), Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
}
return (mMap != null);
}
@Override
public void onMapReady(GoogleMap map) {
}
}
现在的主要活动是:
package location.hive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<Hotel> hotels = DataProvider.hotelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(android.R.id.list);
ArrayAdapter<Hotel> adapter =
new HotelAdapter(this, R.layout.list_item_location, hotels);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
Hotel location = hotels.get(position);
intent.putExtra("city", location.getCity());
startActivity(intent);
}
});
}
}
最后是logcat:
02-26 14:45:07.452 27233-27233/location.hive W/System: ClassLoader referenced unknown path: /data/app/location.hive-1/lib/arm
02-26 14:45:07.472 27233-27233/location.hive I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
02-26 14:45:07.472 27233-27233/location.hive I/InstantRun: starting instant run server: is main process
02-26 14:45:07.562 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/app/location.hive-1/base.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:07.572 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/app/location.hive-1/base.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:07.622 27233-27233/location.hive W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-26 14:45:07.792 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:07.792 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:07.822 27233-27233/location.hive D/AbsListView: Get MotionRecognitionManager
02-26 14:45:07.832 27233-27233/location.hive E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@1c027fd
02-26 14:45:07.832 27233-27233/location.hive E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@5ae4cf2
02-26 14:45:07.832 27233-27233/location.hive E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@5ae4cf2
02-26 14:45:07.862 27233-27233/location.hive D/SecWifiDisplayUtil: Metadata value : none
02-26 14:45:07.872 27233-27233/location.hive D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{7c739c0 I.E...... R.....ID 0,0-0,0}
02-26 14:45:07.872 27233-27276/location.hive D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-26 14:45:07.922 27233-27276/location.hive I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ia10634f51b)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 01/28/16 Thu
Local Branch: ss
Remote Branch:
Local Patches:
Reconstruct Branch:
02-26 14:45:07.922 27233-27276/location.hive D/libEGL: eglInitialize EGLDisplay = 0x9eb9a7c4
02-26 14:45:07.922 27233-27276/location.hive I/OpenGLRenderer: Initialized EGL, version 1.4
02-26 14:45:08.002 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.002 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.022 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.022 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.062 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.072 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.102 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.102 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:08.172 27233-27233/location.hive W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
02-26 14:45:08.192 27233-27233/location.hive W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
02-26 14:45:08.382 27233-27233/location.hive D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
02-26 14:45:08.422 27233-27233/location.hive I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@bd4af43 time:97011894
02-26 14:45:10.632 27233-27233/location.hive D/ViewRootImpl: ViewPostImeInputStage processPointer 0
02-26 14:45:10.682 27233-27233/location.hive D/ViewRootImpl: ViewPostImeInputStage processPointer 1
02-26 14:45:10.752 27233-27233/location.hive I/Timeline: Timeline: Activity_launch_request id:location.hive time:97014225
02-26 14:45:10.822 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/app/location.hive-1/base.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:10.832 27233-27328/location.hive W/ResourceType: For resource 0x7f02005a, entry index(90) is beyond type entryCount(2)
02-26 14:45:10.832 27233-27328/location.hive W/ResourceType: Failure getting entry for 0x7f02005a (t=1 e=90) (error -75)
02-26 14:45:10.842 27233-27328/location.hive E/Resources: RunTimeException
android.content.res.Resources$NotFoundException: Resource ID #0x7f02005a
at android.content.res.Resources.getValue(Resources.java:2558)
at android.content.res.Resources.startRC(Resources.java:1116)
at android.app.ActivityThread$mRunnable.run(ActivityThread.java:3055)
at java.lang.Thread.run(Thread.java:818)
02-26 14:45:10.852 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:10.852 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:10.852 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:10.852 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:10.862 27233-27233/location.hive D/TextView: setTypeface with style : 0
02-26 14:45:10.922 27233-27233/location.hive I/zzbz: Making Creator dynamically
02-26 14:45:10.942 27233-27233/location.hive I/DynamiteModule: Considering local module com.google.android.gms.maps_dynamite:0 and remote module com.google.android.gms.maps_dynamite:18
02-26 14:45:10.942 27233-27233/location.hive I/DynamiteModule: Selected remote version of com.google.android.gms.maps_dynamite, version >= 18
02-26 14:45:10.952 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/app/com.google.android.gms-2/base.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:10.992 27233-27233/location.hive W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000030/n/armeabi
02-26 14:45:11.002 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/data/com.google.android.gms/app_chimera/m/00000030/DynamiteModulesB_GmsCore_prodmnc_xxhdpi_release.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:11.012 27233-27233/location.hive W/ResourcesManager: getTopLevelResources: /data/data/com.google.android.gms/app_chimera/m/00000030/DynamiteModulesB_GmsCore_prodmnc_xxhdpi_release.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:11.112 27233-27233/location.hive I/Google Maps Android API: Google Play services client version: 11910000
02-26 14:45:11.132 27233-27233/location.hive I/Google Maps Android API: Google Play services package version: 11975438
02-26 14:45:11.352 27233-27233/location.hive W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/location.hive/files/DATA_ServerControlledParametersManager.data.location.hive
02-26 14:45:11.492 27233-27233/location.hive D/AbsListView: Get MotionRecognitionManager
02-26 14:45:11.492 27233-27233/location.hive E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@eb5cd14
02-26 14:45:11.492 27233-27233/location.hive E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@95c99bd
02-26 14:45:11.492 27233-27233/location.hive E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@95c99bd
02-26 14:45:11.522 27233-27233/location.hive W/System.err: mkdir failed: EEXIST (File exists) : /storage/emulated/0/Android/data/location.hive/cache/debug
02-26 14:45:11.522 27233-27233/location.hive W/System.err: mkdir failed: EEXIST (File exists) : /storage/emulated/0/Android/data/location.hive/cache
02-26 14:45:11.642 27233-27359/location.hive W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/location.hive/files/event_store_v2_location.hive
02-26 14:45:11.652 27233-27233/location.hive D/AndroidRuntime: Shutting down VM
02-26 14:45:11.652 27233-27233/location.hive E/AndroidRuntime: FATAL EXCEPTION: main
Process: location.hive, PID: 27233
java.lang.RuntimeException: Unable to start activity ComponentInfo{location.hive/location.hive.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at location.hive.DetailActivity.onCreate(DetailActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
02-26 14:45:13.652 27233-27360/location.hive W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
02-26 14:45:13.662 27233-27360/location.hive I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:4
02-26 14:45:13.662 27233-27360/location.hive I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 4
02-26 14:45:13.682 27233-27360/location.hive W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/0000002e/n/armeabi-v7a
02-26 14:45:13.682 27233-27360/location.hive W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/0000002e/n/armeabi
02-26 14:45:13.682 27233-27360/location.hive W/ResourcesManager: getTopLevelResources: /data/data/com.google.android.gms/app_chimera/m/0000002e/GoogleCertificates_GmsCore_prodmnc_xxhdpi_release.apk / 1.0 running in location.hive rsrc of package null
02-26 14:45:13.682 27233-27360/location.hive W/ResourcesManager: getTopLevelResources: /data/data/com.google.android.gms/app_chimera/m/0000002e/GoogleCertificates_GmsCore_prodmnc_xxhdpi_release.apk / 1.0 running in location.hive rsrc of package null
02-26 14:46:05.282 27233-27233/location.hive I/Process: Sending signal. PID: 27233 SIG: 9
这是gradle文件(感谢mohammad ali的反馈):
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "location.hive"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.google.android.gms:play-services:11.8.0'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
testImplementation 'junit:junit:4.12'
}
欢迎所有类型的帮助,如果您有任何提示,请分享给他们,
这是我第二次使用StackOverFlow,如果您什至有关于下次如何改善问题的反馈,也欢迎您
参考方案
使用下面的代码
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class MapViewFragment extends Fragment {
MapView mMapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
if(servicesOK()){
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
Geocoder gc = new Geocoder(this);
List<Address> list;
try {
list = gc.getFromLocationName(hotel.getAddress(), 1);
Address address = list.get(0);
double lat = address.getLatitude();
double lng = address.getLongitude();
LatLng latLong = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLong, 15);
googleMap.moveCamera(update);
MarkerOptions options = new MarkerOptions()
.title(getString(R.string.landon_hotel) + ", " + city)
.position(new LatLng(lat, lng));
googleMap.addMarker(options);
} catch (IOException e) {
Toast.makeText(this, getString(R.string.error_finding_hotel), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d(this.getLocalClassName(), e.getMessage());
}
}
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
public boolean servicesOK() {
int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (result == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(result)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(result, this, DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, getString(R.string.error_connect_to_services),
Toast.LENGTH_SHORT).show();
}
return false;
}
}
单击后退按钮时,滑行加载的图像会重置 - java我正在滑行加载图像。但是问题是,当我们按下后退按钮时,从视图中清除加载的图像。无论我在活动图像视图中还是在recyclerview中加载图像,我都面临着这个问题。我正在加载图像GlideApp.with(this) .load(url) .into(mToolbarAvatar); 参考方案 在代码中添加两行.skipMemoryCache(true) .d…
Android Firebase:将数据保存到数据库 - java我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
getIntent()始终返回null - java此代码启动活动:Intent intent = new Intent(context, GameActivity.class); intent.putExtra("load", true); startActivity(intent); 这是我尝试获得新活动的意图的方法:private Intent intent = this.getIn…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…