2012-10-13 71 views
1

我已经从here了解谷歌地图API,也有人说什么.. 我mainactivity代码如下:无法运行Android应用

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] 

我不知道什么是错误的,但这显示Sorry, the app stopped unexpectedly。 我使用2.3.3平台和API等级10avd上运行它。 Plz纠正我...

+0

您能给我们从logcat中或Eclipse控制台一些信息呢? –

+0

其实我正在使用netbeans ..它编译的很好......控制台输出已被添加.. – nitish712

+0

检查[logcat](http://developer.android.com/tools/help/logcat.html)和发布您收到的logcat输出。 – Rajesh

回答

2

我使用你的代码并使它可以运行,下面是运行代码。只有改变你的地图API密钥:试试吧...和使用仿真器和Android版本,如Android 2.2谷歌API 谷歌API还包括缩放控件: 的main.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> 

MainActivity.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 
+0

最后你给的代码已经工作了...... !!! thanx很多...我实际上是一个初学者,但我会尽力解决这个问题。与我的prev。代码肯定..未来... :) :) – nitish712

0

您的<uses-library>标记是在错误的地方。它应该在<application>标记中,而不是<activity>标记。尝试使用:

<?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" > 
<uses-library android:name="com.google.android.maps"/> 
    <activity android:name="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> 
</application> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

此外,请确保您使用安装了Google API的模拟器。

+0

这是没有任何区别.... – nitish712

0

1)检查你的目标是Google api 2.3.3。

2)检查您的密钥是否正确。

3)像这样检查你的清单。

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".ShowMapActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

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

    </application> 

4)如果你没有得到解决方案,然后再找到你的密钥并使用它。

相关问题