2010.12.10(3)——— android MapView 以及 Unable to instantiate activity ComponentInfo问题
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.huitu"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" 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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk android:minSdkVersion="8" />
</manifest>
注意
1、要加入包
<uses-library android:name="com.google.android.maps" />
2、要加入权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
layout文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapview"
android:clickable="true"
android:enabled="true"
android:apiKey="0yhxK-DcK-8qhPG14wiKUL-zAkHqPDDVkk5W1hw"
/>
<LinearLayout
android:id="@+id/zoomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/mapview"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
android:apiKey="0yhxK-DcK-8qhPG14wiKUL-zAkHqPDDVkk5W1hw"
就是你申请的map api key
LinearLayout 是用来实现Zoom功能的
MainActivity
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ZoomControls;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MainActivity extends MapActivity {
private LinearLayout linearLayout;
private MapView mapView;
private ZoomControls mZoom;
private LocationManager locationManager;
private List<Overlay> mapOverlays;
private Drawable drawable;
private MyPositionItemizedOverlay itemizedOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mZoom = (ZoomControls) mapView.getZoomControls();
linearLayout.addView(mZoom);
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.red);
itemizedOverlay = new MyPositionItemizedOverlay(drawable);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
//Location location = locationManager.getLastKnownLocation(provider);
LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location) {
double lat=0.0;
double lng = 0.0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
}else{
lat = 39.92;
lng = 116.46;
}
Toast.makeText(MainActivity.this, lat+"==="+lng, 0).show();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
MapController controller = mapView.getController();
controller.animateTo(point);
controller.setZoom(21);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
locationManager.requestLocationUpdates(provider, 500, 5,
ll);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 按下键盘上返回按钮
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setMessage("确定退出系统吗?")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
}).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 或者下面这种方式
//System.exit(0);
//建议用这种
android.os.Process.killProcess(android.os.Process.myPid());
}
protected boolean isRouteDisplayed() {
return false;
}
}
注意:
1、一定要实现MapActivity 并且 一定要重写
protected boolean isRouteDisplayed() {
return false;
}
这个方法
这个实现 当坐标变化时 会在地图上显示新的位置
Unable to instantiate activity ComponentInfo问题
这个问题 就是我上面写的注意 你只要都注意的 应该就不会出这个问题了
但是 其实 说出来 怕你们笑话 我遇到这个问题 折磨了我 好几个小时 最后的原因 让人哭笑不得
<uses-library android:name="com.google.android.maps" />
我写成了
<
users-library android:name="com.google.android.maps" />
看清楚了
uses -------users
凌乱了。。。。。杯具了。。。