android hellomap示例给出异常

时间:2010-06-14 19:35:05

标签: android

 06-14 22:13:33.992: ERROR/AndroidRuntime(331): Uncaught handler: thread main exiting due to uncaught exception
06-14 22:13:34.031: ERROR/AndroidRuntime(331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.HelloMap}: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.os.Looper.loop(Looper.java:123)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread.main(ActivityThread.java:4363)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at java.lang.reflect.Method.invokeNative(Native Method)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at java.lang.reflect.Method.invoke(Method.java:521)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at dalvik.system.NativeStart.main(Native Method)
06-14 22:13:34.031: ERROR/AndroidRuntime(331): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.maps.MapView
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.Activity.setContentView(Activity.java:1622)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.example.HelloMap.onCreate(HelloMap.java:16)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     ... 11 more
06-14 22:13:34.031: ERROR/AndroidRuntime(331): Caused by: java.lang.reflect.InvocationTargetException
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.google.android.maps.MapView.<init>(MapView.java:237)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     ... 21 more
06-14 22:13:34.031: ERROR/AndroidRuntime(331): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.google.android.maps.MapView.<init>(MapView.java:281)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     at com.google.android.maps.MapView.<init>(MapView.java:254)
06-14 22:13:34.031: ERROR/AndroidRuntime(331):     ... 25 more

我完成了所有配置。

这是我的清单文件:

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

     <uses-sdk android:minSdkVersion="3" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
        <activity android:name="com.example.HelloMap"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

and my layout file main.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/mainlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="*************"
    />
</RelativeLayout>

我的Hellomap.java

public class HelloMap extends Activity {
    LinearLayout linearLayout;
    MapView mapView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);

    }



    protected boolean isRouteDisplayed() {
        return false;
    }   
}

我想错过的东西,或者问题可能是2.1 Google API?

4 个答案:

答案 0 :(得分:17)

我自己的回答

只看异常日志

最后一行

MapViews can only be created inside instances of MapActivity. 06-14 22:13:34.031: ERROR/AndroidRuntime(331): at com.google.android.maps.MapView.(MapView.java:281) 06-14 22:13:34.031: 

这意味着您需要使用MapActivity扩展您的活动而不是活动我猜大多数人都知道但我现在知道了 :)

答案 1 :(得分:2)

我遇到了这个问题并通过以下两个步骤解决了这个问题:

1)将以下行放在AndroidManifest.xml文件的应用程序(重要)元素中。

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

2)扩展MapActivity而不是Activity。

享受!

答案 2 :(得分:2)

这里我将Activity更改为MapActivity。

所以不是

public class HelloMap extends Activity {

将其更改为

public class HelloMap extends MapActivity {

答案 3 :(得分:0)

如果您是使用谷歌地图的新手并且已经设置了xml和活动并且遇到了这种类型的错误,那么这意味着您没有从MapsActivity扩展您的类,所以从mapsactivity扩展您的类,它将开始工作了!

//错误使用活动

public class A extends Actvitiy
{   
 }
 //correct usage

public class A extedns MapsActivity {
 //your all program...
 }