Jetpack Compose中Google Places Autocomplete崩溃:Intent缺少Place数据
问题:Jetpack Compose集成Google Places Autocomplete输入字符崩溃
我正在开发一款基于Jetpack Compose的应用,集成Google Places Autocomplete实现地点搜索功能。但启动Autocomplete Intent后,输入一个字符应用就崩溃了,报错信息为:java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't.
ViewModel代码
init { if (!Places.isInitialized()) { Places.initialize(context, "$API_KEY", Locale.US); } } val field = listOf(Place.Field.NAME, Place.Field.LAT_LNG)
UI代码
val context = LocalContext.current val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, viewModel.field).build(context) val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { if(it.resultCode == 2){ val place = Autocomplete.getPlaceFromIntent(it.data) val latLng = place.latLng Log.d("place LatLng: ", latLng.toString()) // move the camera position of the map // cameraPositionState.move(CameraUpdateFactory.newLatLngZoom(lonLat, 15f)) } }
Logcat日志
FATAL EXCEPTION: main Process: com.example.trip, PID: 19245 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=988884199, result=2, data=Intent { (has extras) }} to activity {com.example.trip/com.example.trip.MainActivity}: java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't. at android.app.ActivityThread.deliverResults(ActivityThread.java:5015) at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.IllegalArgumentException: Intent expected to contain a Place, but doesn't. at com.google.android.libraries.places.internal.zzha.zze(com.google.android.libraries.places:places@@2.6.0:1) at com.google.android.libraries.places.internal.zzfo.zzb(com.google.android.libraries.places:places@@2.6.0:3) at com.google.android.libraries.places.widget.Autocomplete.getPlaceFromIntent(com.google.android.libraries.places:places@@2.6.0:1) at com.example.trip.ui.theme.tripSection.createTrip.CreateTripScreenKt$CreateTripScreen$2$1$launcher$1.invoke(CreateTripScreen.kt:116) at com.example.trip.ui.theme.tripSection.createTrip.CreateTripScreenKt$CreateTripScreen$2$1$launcher$1.invoke(CreateTripScreen.kt:113) at androidx.activity.compose.ActivityResultRegistryKt$rememberLauncherForActivityResult$1$1.onActivityResult(ActivityResultRegistry.kt:105) at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:392) at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:351) at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:638) at android.app.Activity.dispatchActivityResult(Activity.java:8310) at android.app.ActivityThread.deliverResults(ActivityThread.java:5008) at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Gradle依赖
//Google places api implementation 'com.google.android.libraries.places:places:2.6.0'
解决方案
1. 修正结果码判断逻辑
不要用硬编码的2作为成功结果码,Google Places Autocomplete的成功返回码是Activity.RESULT_OK(对应值-1)。你当前的判断会错误捕获输入字符时的中间状态,此时Intent内并没有Place数据,调用Autocomplete.getPlaceFromIntent必然崩溃。
修改后的回调代码:
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val data = result.data ?: return@rememberLauncherForActivityResult val place = Autocomplete.getPlaceFromIntent(data) val latLng = place.latLng Log.d("place LatLng: ", latLng.toString()) // 后续地图操作 } }
2. 增加空值防护
即使结果码正确,也要先判断result.data是否为空,避免空指针异常。
3. 验证API密钥有效性
确保$API_KEY替换为有效的Google Places API密钥,且该密钥已启用Places API服务,同时配置了正确的应用签名和包名限制。
4. 确认初始化时机
确保Places初始化在主线程执行,且仅初始化一次。ViewModel的init块是合适的位置,但要保证传入的context是Application Context,避免内存泄漏。
内容的提问来源于stack exchange,提问作者heet kanabar
相关产品推荐
相关产品推荐

