2013-05-12 26 views
0

嗨〜我剛學了一段Android應用程序。我的項目是使用谷歌地圖找到我的位置(GPS)logcat:致命的例外:主要在GPS應用

問題是應用程序無法在我的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。我已經把三個Internet權限在我的清單

<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> 

的Java:

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

檢查你的活動被定義在Manifest文件中,並且您在Java文件 – 2013-05-12 12:27:53

+0

中使用了正確的導入和包名。對不起,我不明白。但是我發佈我的代碼讓我們更清楚! – Kyle 2013-05-12 13:13:33

+0

您是否遵循Google Maps API [指南](https://developers.google.com/maps/documentation/android/start#specify_settings_in_the_application_manifest)?我可以看到丟失的信息(比如元數據) – 2013-05-12 13:34:59

回答

0

添加此行到您的清單文件中的應用程序標籤是這樣的:

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

我補充說,但它仍然無法工作! (我在應用程序和infrom活動之間添加〜) – Kyle 2013-05-12 13:11:57

+0

更新清單中寫入的代碼並更新Logcat。 logcat內部的消息必須已更改。嘗試清理你的項目並重新構建它。 – 2013-05-12 13:15:41