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

Android Studio Java:首页按钮启动地图Activity崩溃问题求助

解决Fragment启动MapsActivity崩溃问题

问题排查与修复步骤

  • API_KEY元数据名称错误:Manifest里将com.google.android.geo.API_KEY误写为come.google.android.geo.API_KEY(多了一个字母e),导致地图无法读取有效密钥,直接引发崩溃。
  • Intent Filter分类错误:MapsActivity的intent-filter中,category写成了android.intent.category.default.DEFAULT,正确值应为android.intent.category.DEFAULT,多余的default.会导致Intent解析异常。
  • 冗余权限声明:package_name.permission.MAPS_RECEIVE是旧版Google Maps的废弃权限,且未替换为实际包名,直接删除即可。
  • 空指针防护缺失:Fragment中调用getActivity()可能返回null(如Fragment脱离Activity时),需添加非空判断避免崩溃。

修正后的Manifest代码

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31" >

        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".MapsActivity"
            android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBgSEI9Xvh3TicLaNj8FigA4iRyP1jdEVs" />

        <activity
            android:name=".SecondActivity"
            android:exported="true" />

        <activity
            android:name=".MainActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

修正后的Fragment代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_fragment1,container,false);

    bt = v.findViewById(R.id.launchmap);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (getActivity() != null) {
                Intent intent = new Intent(getActivity(), MapsActivity.class);
                startActivity(intent);
            }
        }
    });

    return v;
}

内容的提问来源于stack exchange,提问作者Ahmed Zaidan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 00:35:25