应用程序在包含Google Map的打开页面上崩溃

时间:2014-03-19 12:40:44

标签: java android google-maps

我有一个带主页的应用程序,其中一个页面/活动应该包含一个地图但是当我点击按钮打开它时我的应用程序崩溃了。我该怎么办呢?

清单 -

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!-- External storage for caching. -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- My Location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Maps API needs OpenGL ES 2.0. -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

         <!-- You must insert your own Google Maps for Android API v2 key in here. -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="mykey" />

        <activity
            android:name="com.example.edinburghnights1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.edinburghnights1.Map"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

            </activity>

            <activity
            android:name="com.example.edinburghnights1.WhatsOn"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

           </activity>

            <activity
            android:name="com.example.edinburghnights1.ClubList"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>

           </activity>



    </application>

</manifest>

XML -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:orientation="vertical" >

    <Button
        android:id="@+id/back5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:text="Back"
        android:textColor="#FFFFFF" />

  <fragment 

  android:id="@+id/map"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  class="com.google.android.gms.maps.SupportMapFragment"/>

</LinearLayout>

Java -

package com.example.edinburghnights1;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;

public class Map extends Activity{ // extends activity so it can be linked to rest of app


        public void onCreate(Bundle savedInstanceState) {

                                    super.onCreate(savedInstanceState);
                                    setContentView(R.layout.mapsxml); //layout from mapsxml
                                    Button b1=(Button)findViewById(R.id.back5); // name buttons to be used


                                      b1.setOnClickListener(new View.OnClickListener() {

                                        public void onClick(View v) { //action when clicked
                                            Intent myintent2 = new Intent(Map.this,MainActivity.class);
                                            startActivity(myintent2);

                                        }
                                    });
                                    private void initilizeMap() {

                                        googleMap =   ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

                                          // check if map is created successfully or not

                                          }
        }}

2 个答案:

答案 0 :(得分:1)

你应该改变这个

 googleMap =   ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

googleMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

还需要将您的活动扩展到FragmentActivity,例如:

  public class Map extends android.support.v4.app.FragmentActivity

答案 1 :(得分:0)

将您的googleMap演员阵容更改为SupportMapFragment

googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

googleMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();