You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Expo管理的React Native安卓应用调用Location.getCurrentPositionAsync()后崩溃

Expo托管React Native安卓应用调用Location.getCurrentPositionAsync后崩溃

我开发的Expo托管式React Native安卓应用,在调用Location.getCurrentPositionAsync()获取当前位置后直接崩溃。注释掉该语句及相关依赖代码后,应用恢复正常运行,因此确定是expo-location的这个静态方法导致了安卓端崩溃。

附崩溃截图:应用触发位置获取操作后显示崩溃错误界面

相关代码

import React, { useState, useEffect } from "react"
import * as Location from "expo-location"
import { FontAwesome } from "@expo/vector-icons"
import MapView, { Marker } from "react-native-maps"
import { View, Text } from "react-native"

export const LocationScreen = ({ navigation }) => {
  const [location, setLocation] = useState(null)
  const [error, setError] = useState(null)

  useEffect(() => {
    ;(async () => {
      const { status } = await Location.requestForegroundPermissionsAsync()
      if (status !== "granted") {
        setError("Permission to access location was denied")
        return
      }
      if (status === "granted") {
        const locate = await Location.getCurrentPositionAsync()
        setLocation(locate.coords)
      }
    })()
  }, [])
 
  return (
    <View style={styles.mapContainer}>
      <MapView style={styles.map}>
        {location ? (
          <Marker coordinate={location} title="My location">
            <FontAwesome name="map-marker" size={40} color={colors.palette.map} />
          </Marker>
        ) : (
          <Text>{error}</Text>
        )}
      </MapView>
    </View>
  )
}

依赖版本

  • "expo": "^46.0.0"
  • "expo-location": "~14.3.0"
  • "react-native": "0.69.7"
  • "react": "18.0.0"

内容的提问来源于stack exchange,提问作者David Tembo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:55:24