嗨〜我刚刚学习Android应用程序了一段时间。我的项目是使用googlemap查找我的位置(GPS)
问题是app无法在我的android中运行。它显示“抱歉goooglegps(我的项目名称)已停止!!”
这是我的logcat。
05-10 11:56:21.146: E/Trace(2547): error opening trace file: No such file or directory (2)
05-10 11:56:21.226: W/dalvikvm(2547): Unable to resolve superclass of Lcom/example/googlegps/MainActivity; (481)
05-10 11:56:21.226: W/dalvikvm(2547): Link of class 'Lcom/example/googlegps/MainActivity;' failed
05-10 11:56:21.236: W/dalvikvm(2547): threadid=1: thread exiting with uncaught exception (group=0x40c54540)
05-10 11:56:21.236: E/AndroidRuntime(2547): FATAL EXCEPTION: main
05-10 11:56:21.236: E/AndroidRuntime(2547): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.googlegps/com.example.googlegps.MainActivity}: java.lang.ClassNotFoundException: com.example.googlegps.MainActivity
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2271)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread.access$600(ActivityThread.java:151)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.os.Looper.loop(Looper.java:155)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread.main(ActivityThread.java:5511)
05-10 11:56:21.236: E/AndroidRuntime(2547): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:56:21.236: E/AndroidRuntime(2547): at java.lang.reflect.Method.invoke(Method.java:511)
05-10 11:56:21.236: E/AndroidRuntime(2547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
05-10 11:56:21.236: E/AndroidRuntime(2547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
05-10 11:56:21.236: E/AndroidRuntime(2547): at dalvik.system.NativeStart.main(Native Method)
05-10 11:56:21.236: E/AndroidRuntime(2547): Caused by: java.lang.ClassNotFoundException: com.example.googlegps.MainActivity
05-10 11:56:21.236: E/AndroidRuntime(2547): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-10 11:56:21.236: E/AndroidRuntime(2547): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-10 11:56:21.236: E/AndroidRuntime(2547): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.Instrumentation.newActivity(Instrumentation.java:1069)
05-10 11:56:21.236: E/AndroidRuntime(2547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
05-10 11:56:21.236: E/AndroidRuntime(2547): ... 11 more
这个问题让我疯了很长时间!!我不知道哪个部分有问题。 有人能帮助我吗? ><
PS。我已经在我的清单中放了三个互联网权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCES_FINE_LOCATION" />
<uses-permission android:name = "android.permission.ACCESS_MOCK_LOCATION" />
这是我的代码! 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlegps"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCES_FINE_LOCATION" />
<uses-permission android:name = "android.permission.ACCESS_MOCK_LOCATION" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlegps.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>
</manifest>
活性:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner android:id="@+id/spnLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/prompt_select_location"
/>
<com.google.android.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="0k2yDM6Bs-MFb8mDCA265joWiZOKsJNFbUjqw0w"
android:clickable="true" />
</RelativeLayout>
爪哇:
package com.example.googlegps;
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 android.util.*;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends MapActivity implements LocationListener {
private LocationManager mLocationMgr;
private String mBestLocationProv;
private static String[][] locations = {
{"台灣大學", "25.019943,121.542353"},
{"清華大學", "24.795621,120.998153"},
{"交通大學", "24.791704,121.003341"},
{"成功大學", "23.000875,120.218017"}};
private Spinner mSpnLocation;
private MapView mMapView;
private MapController mMapCtrl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViewComponent();
setMapLocation();
mLocationMgr = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria c = new Criteria();
mBestLocationProv = mLocationMgr.getBestProvider(c, true);
}
private void setupViewComponent() {
mSpnLocation = (Spinner) this.findViewById(R.id.spnLocation);
mMapView = (MapView) findViewById(R.id.map);
mMapCtrl = mMapView.getController();
mMapCtrl.setZoom(18);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item);
for (int i = 0; i < locations.length; i++)
adapter.add(locations[i][0]);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpnLocation.setAdapter(adapter);
mSpnLocation.setOnItemSelectedListener(mSpnLocationOnItemSelLis);
}
private OnItemSelectedListener mSpnLocationOnItemSelLis = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
setMapLocation();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
public void setMapLocation() {
int iSelect = mSpnLocation.getSelectedItemPosition();
String[] sLocation = locations[iSelect][1].split(",");
double dLat = Double.parseDouble(sLocation[0]); // 南北緯
double dLon = Double.parseDouble(sLocation[1]); // 東西經
GeoPoint gp = new GeoPoint((int)(dLat * 1e6), (int)(dLon * 1e6));
mMapCtrl.animateTo(gp);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
mMapCtrl.animateTo(new GeoPoint(
(int)(location.getLatitude() * 1e6),
(int)(location.getLongitude() * 1e6)));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
mLocationMgr.removeUpdates(this);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
mLocationMgr.requestLocationUpdates(mBestLocationProv, 60000, 1, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Criteria c = new Criteria();
mBestLocationProv = mLocationMgr.getBestProvider(c, true);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
mLocationMgr.removeUpdates(this);
super.onStop();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mLocationMgr.requestLocationUpdates(mBestLocationProv, 60000, 1, this);
}
}
答案 0 :(得分:0)
将此行添加到应用程序标记内的清单文件中,如下所示:
<application>
<uses-library android:name="com.google.android.maps" />
</application>