无法在真实设备上运行google maps v2

时间:2013-02-05 21:02:55

标签: android google-maps

我正在尝试制作一个简单的应用,在我的设备上显示谷歌地图。我在我的Alcatel OT 990安卓设备(v.2.2.2)中运行它,但它显示错误消息“应用程序已意外停止....然后强制关闭”。我相信我做的一切都是正确的,因为我按照有效的youtube教程中的说明操作。 这是我的代码(我已经包含Android支持库,Google Play服务库和android.jar):

清单

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<permission
    android:name="com.xristina.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.xristina.maps.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.tutorialgooglemaps.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>
    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="*************my key here*********************"/>
</application>

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

MainActivity.java

package com.xristina.maps;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends android.support.v4.app.FragmentActivity {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpMapIfNeeded();

}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}

有什么想法吗?

Ι添加了@BBonDoo建议的代码,logcat结果是:

02-06 16:59:34.807: W/dalvikvm(2735): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
02-06 16:59:34.817: E/AndroidRuntime(2735): FATAL EXCEPTION: main
02-06 16:59:34.817: E/AndroidRuntime(2735): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.xristina.maps/com.example.tutorialgooglemaps.MainActivity}: java.lang.ClassNotFoundException: com.example.tutorialgooglemaps.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.xristina.maps-2.apk]
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2603)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2697)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2051)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.os.Looper.loop(Looper.java:123)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread.main(ActivityThread.java:4645)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at java.lang.reflect.Method.invoke(Method.java:521)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at dalvik.system.NativeStart.main(Native Method)
02-06 16:59:34.817: E/AndroidRuntime(2735): Caused by: java.lang.ClassNotFoundException: com.example.tutorialgooglemaps.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.xristina.maps-2.apk]
02-06 16:59:34.817: E/AndroidRuntime(2735):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
02-06 16:59:34.817: E/AndroidRuntime(2735):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2595)
02-06 16:59:34.817: E/AndroidRuntime(2735):     ... 11 more

我还必须说这个代码没有包含在我研究过的教程中 http://www.youtube.com/watch?v=mRnEkP2q4fohttp://www.youtube.com/watch?v=OZRgiwGfvSQ

2 个答案:

答案 0 :(得分:2)

这是你的问题:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="myApiKey"/>

这个“myApiKey”被认为是您从Google API控制台获得的API密钥。

答案 1 :(得分:0)

[回答新] ...添加更新]

你的Logcat很好。它的含义如下;

请查看两张图片,并将您的包裹名称(①)与正确的(②)匹配。

This is your manifest file

This is your java file

请使用java文件中的包名替换清单文件中的包名。

清单文件中的代码是正确的;

<Activity
     android:name="com.xristina.maps.MainActivity"
     ...

[回答旧]

您没有完成编写足以在MainActivity.java中显示Google地图的代码。请使用下面显示的代码填写您的代码,这是您必须了解的基本代码,以便在Android平台中显示Google地图:

public class MianActivity extends android.support.v4.app.FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMapIfNeeded();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}