android - 无法运行安卓应用

标签 android google-maps

我一直在从 here 学习 google maps api并完全按照所说的去做.. 我的mainactivity代码如下:

package com.map.place;

import android.app.Activity;
import android.os.Bundle;
import com.google.android.maps.MapActivity;

public class MainActivity extends MapActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0Ove92B2I7FZi4SR2YX89jA374Wcg6-cJ33VwUg"
/>

最后,manifest 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.map.place"
  android:versionCode="1"
  android:versionName="1.0">
      <uses-sdk android:minSdkVersion="10" />
<application android:label="@string/app_name" >
    <activity android:name="MainActivity"
              android:label="@string/app_name">
                  <uses-library android:name="com.google.android.maps" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest> 

logcat 控制台显示如下:

E/AndroidRuntime(  975): FATAL EXCEPTION: main
E/AndroidRuntime(  975): java.lang.RuntimeException: Unable to instantiate activ
ity ComponentInfo{com.map.place/com.map.place.MainActivity}: java.lang.ClassNotF
oundException: com.map.place.MainActivity in loader dalvik.system.PathClassLoade
r[/data/app/com.map.place-2.apk]

我不知道出了什么问题,但这正在显示抱歉,应用程序意外停止。 我在 2.3.3 平台和 API 级别 10avd 上运行它。请纠正我...

最佳答案

我使用您的代码并使其可运行,下面是运行代码。仅更改您的 Map API key :尝试一下...并使用模拟器和带有 google API 的 android 版本,例如 android 2.2 google API 它还包括缩放控件: 主.xml:

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

    <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="0twJXMQDHBfxjMnb77nwRzKs29kGZdRkcG3_Z8Q" />

    <LinearLayout android:id="@+id/zoom" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_centerHorizontal="true" 
       /> 


</RelativeLayout>

您的 manifest.xml 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.map.place"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
 <uses-permission android:name="android.permission.INTERNET" />

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <uses-library
        android:name="com.google.android.maps"
        android:required="true" />
 </application>
 </manifest>

主要 Activity .java:

package com.map.place;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class MainActivity extends MapActivity{

//===========
MapView mapView;
public static String latitude,longitude;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mapView = (MapView) findViewById(R.id.mapView);
mapView.setSatellite(false);
LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();

zoomLayout.addView(zoomView, new     
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mapView.invalidate();

}

// Below is the code when the user presses the number 3 on the keyboard the
// map  will zoom in into the next level. Pressing number 1 will zoom out one
// level.

public boolean onKeyDown(int keyCode, KeyEvent event) {
MapController mc = mapView.getController();
switch (keyCode) {
case KeyEvent.KEYCODE_3:
    mc.zoomIn();
    break;
case KeyEvent.KEYCODE_1:
    mc.zoomOut();
    break;
}
return super.onKeyDown(keyCode, event);
}


@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

}
//By Umesh Suryawanshi

关于android - 无法运行安卓应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870401/

相关文章:

php - 将 mysql 页面更改为 mysqli,现在 google map initMap 函数抛出错误?

android - GridLayout 在整个屏幕上没有均匀地间隔 child

java - Android - 任务 ':app:compileDebugJava' 执行失败,预期为 ';'

java - 如何在 SlidingTab 中放置图标?

ios - 为当前位置自定义 Google map 蓝点

java - 如何将字符串值发送到另一个 Activity ?

javascript - html 而不是谷歌地图标记图像

Android maps.googleapis.com,检索 JSON 数据时出现问题

java - 为每 5 个项目在 recyclerview 中添加广告?

android - 适配器中有多种不同的布局