Android地图:FATAL EXCEPTION:主要错误

时间:2014-01-22 10:24:16

标签: android eclipse google-maps android-fragments maps

  

您好。我可以问一下这个错误是什么。我的应​​用程序就像谷歌地图。当我运行该应用程序。 logcat中有一些错误。我不知道怎么会出现这个错误。任何人都可以告诉我们如何修复它。我需要你的专业知识。我已经发布了清单文件myactivity.java和logcat跟踪。

Android清单文件

     <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="edu.usf.cutr.opentripplanner.android"
          android:versionCode="8"
          android:versionName="2.0.1" >
          <uses-sdk android:minSdkVersion="8" 
                    android:targetSdkVersion="13"/>

          <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
          <uses-permission android:name="android.permission.INTERNET" />

          <!-- To plan trip from/to contact address -->
          <uses-permission android:name="android.permission.READ_CONTACTS" />

          <!-- Android Maps API V2 -->
          <permission
               android:name="edu.usf.cutr.opentripplanner.android.permission.MAPS_RECEIVE"
               android:protectionLevel="signature"/>
          <uses-permission android:name="edu.usf.cutr.opentripplanner.android.permission.MAPS_RECEIVE"/>    
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
        <!-- The following permission is not required to use
             Google Maps Android API v2, but is recommended. -->
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

        <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true"/>

          <supports-screens
              android:anyDensity="true"
              android:largeScreens="true"
              android:normalScreens="true"
              android:smallScreens="true" />

          <application
              android:name=".OTPApp"  
              android:icon="@drawable/ic_launcher_opentripplanner"
              android:label="@string/app_name"
              android:allowBackup="true">
              <activity
                  android:name=".MyActivity"
                  android:label="@string/app_name"
                  android:launchMode="singleTop"
                android:theme="@style/MyTheme"
                  android:windowSoftInputMode="stateHidden" >
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />

                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
              <activity android:name=".SettingsActivity" 
                android:theme="@style/MyTheme"
                android:windowSoftInputMode="stateHidden" >
              </activity>


              <service android:name=".NavigationService" />
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyD5XadJhAK7jIKFz5TcT7t-gsWw_OTvlBI"/>
          </application>
      </manifest>

** MyActivity.java

    package edu.usf.cutr.opentripplanner.android;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;

    import org.opentripplanner.v092snapshot.api.model.Itinerary;
    import org.opentripplanner.v092snapshot.api.model.Leg;

    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.util.Log;
    import android.widget.Toast;

    import com.google.android.gms.maps.model.LatLng;

    import edu.usf.cutr.opentripplanner.android.fragments.DirectionListFragment;
    import edu.usf.cutr.opentripplanner.android.fragments.MainFragment;
    import edu.usf.cutr.opentripplanner.android.listeners.DateCompleteListener;
    import edu.usf.cutr.opentripplanner.android.listeners.OnFragmentListener;
    import edu.usf.cutr.opentripplanner.android.model.OTPBundle;

    public class MyActivity extends FragmentActivity implements OnFragmentListener{

      private List<Leg> currentItinerary = new ArrayList<Leg>();

      private List<Itinerary> currentItineraryList = new ArrayList<Itinerary>();

      private int currentItineraryIndex = -1;

      private OTPBundle bundle = null;

      private MainFragment mainFragment;

      private String TAG = "OTP";

      private String currentRequestString="";

      private boolean isButtonStartLocation = false;

      DateCompleteListener dateCompleteCallback;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      //  bundle = (OTPBundle)getLastCustomNonConfigurationInstance();
        setContentView(R.layout.activity);

        if (savedInstanceState != null) {
          mainFragment = (MainFragment) getSupportFragmentManager().findFragmentByTag(OTPApp.TAG_FRAGMENT_MAIN_FRAGMENT);//recuperar o tag adecuado e pillar ese fragment

          }

        if(savedInstanceState==null){
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          mainFragment = new MainFragment();
          fragmentTransaction.replace(R.id.mainFragment, mainFragment, OTPApp.TAG_FRAGMENT_MAIN_FRAGMENT);
          fragmentTransaction.commit();
        }

      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
        switch(requestCode) {
        case OTPApp.SETTINGS_REQUEST_CODE: 
          if (resultCode == RESULT_OK) {
            boolean shouldRefresh = data.getBooleanExtra(OTPApp.REFRESH_SERVER_RETURN_KEY, false);
            boolean changedSelectedCustomServer = data.getBooleanExtra(OTPApp.CHANGED_SELECTED_CUSTOM_SERVER_RETURN_KEY, false);
            boolean changedTileProvider = data.getBooleanExtra(OTPApp.CHANGED_MAP_TILE_PROVIDER_RETURN_KEY, false);

            //        Toast.makeText(this, "Should server list refresh? " + shouldRefresh, Toast.LENGTH_LONG).show();
            if(shouldRefresh){
              mainFragment.setNeedToRunAutoDetect(true);
              mainFragment.setNeedToUpdateServersList(true);
            }
            if(changedSelectedCustomServer){
              mainFragment.updateSelectedServer();
            }
            if(changedTileProvider){
              mainFragment.updateOverlay(null);
            }
            break;
          }
        case OTPApp.CHOOSE_CONTACT_REQUEST_CODE:
          if(resultCode == RESULT_OK){
    //        Log.v(TAG, "CHOOSE CONTACT RESULT OK");

            Uri contactData = data.getData();
            Cursor c =  managedQuery(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                mainFragment.setTextBoxLocation(address, isButtonStartLocation);
                mainFragment.processAddress(isButtonStartLocation, address, false);
            }

            break;
          }
        }
      }

      @Override
      protected void onDestroy() {    

        mainFragment = null;

        Log.d(TAG, "Released mainFragment with map in MyActivity.onDestroy()");

        super.onDestroy();
      }

      @Override
      public void onItinerariesLoaded(List<Itinerary> itineraries) {
        // TODO Auto-generated method stub
        currentItineraryList.clear();
        currentItineraryList.addAll(itineraries);
      }

      @Override
      public void onItinerarySelected(int i) {
        // TODO Auto-generated method stub
        if(i >= currentItineraryList.size()) return;

        currentItineraryIndex = i;
        currentItinerary.clear();
        currentItinerary.addAll(currentItineraryList.get(i).legs);
      }

      @Override
      public List<Leg> getCurrentItinerary() {
        // TODO Auto-generated method stub
        return currentItinerary;
      }

      @Override
      public void onSwitchedToDirectionFragment() {
        // TODO Auto-generated method stub
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        Fragment directionFragment = new DirectionListFragment();
        transaction.add(R.id.mainFragment, directionFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.addToBackStack(null);

        transaction.commit();
      }

      @Override
      public OTPBundle getOTPBundle() {
        // TODO Auto-generated method stub
        return bundle;
      }

      @Override
      public void setOTPBundle(OTPBundle b) {
        // TODO Auto-generated method stub
        this.bundle = b;
        this.bundle.setCurrentItineraryIndex(currentItineraryIndex);
        this.bundle.setItineraryList(currentItineraryList);
      }

      @Override
      public void onSwitchedToMainFragment(Fragment f) {
        // TODO Auto-generated method stub
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.remove(f);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fm.popBackStack();
        transaction.commit();

        mainFragment.showRouteOnMap(currentItinerary, true);
      }

      @Override
      public void setCurrentRequestString(String url) {
        currentRequestString = url;
      }

      @Override
      public String getCurrentRequestString() {
        return currentRequestString;
      }

      @Override
      public void zoomToLocation(LatLng location){
        mainFragment.zoomToLocation(location);
      }

      @Override
      public List<Itinerary> getCurrentItineraryList() {
        // TODO Auto-generated method stub
        return currentItineraryList;
      }

      @Override
      public int getCurrentItineraryIndex() {
        // TODO Auto-generated method stub
        return currentItineraryIndex;
      }

      /**
       * @return the isButtonStartLocation
       */
      public boolean isButtonStartLocation() {
        return isButtonStartLocation;
      }

      /**
       * @param isButtonStartLocation the isButtonStartLocation to set
       */
      public void setButtonStartLocation(boolean isButtonStartLocation) {
        this.isButtonStartLocation = isButtonStartLocation;
      }

      public void setDateCompleteCallback(DateCompleteListener callback){
        this.dateCompleteCallback = callback;
      }

      public void onDateComplete(Date tripDate, boolean scheduleType) {
        dateCompleteCallback.onDateComplete(tripDate, scheduleType);
      }

    }

Logcat错误

01-22 08:29:37.038: E/AndroidRuntime(3278): FATAL EXCEPTION: main
01-22 08:29:37.038: E/AndroidRuntime(3278): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.usf.cutr.opentripplanner.android/edu.usf.cutr.opentripplanner.android.MyActivity}: android.view.InflateException: Binary XML file line #32: Error inflating class fragment
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread.access$600(ActivityThread.java:165)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.os.Handler.dispatchMessage(Handler.java:107)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.os.Looper.loop(Looper.java:194)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread.main(ActivityThread.java:5391)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at java.lang.reflect.Method.invoke(Method.java:525)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at dalvik.system.NativeStart.main(Native Method)
01-22 08:29:37.038: E/AndroidRuntime(3278): Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class fragment
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at edu.usf.cutr.opentripplanner.android.fragments.MainFragment.onCreateView(MainFragment.java:315)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1232)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.Activity.performStart(Activity.java:5132)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
01-22 08:29:37.038: E/AndroidRuntime(3278):     ... 11 more
01-22 08:29:37.038: E/AndroidRuntime(3278): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4132500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.SupportMapFragment$b.eb(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.dynamic.a.a(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:284)
01-22 08:29:37.038: E/AndroidRuntime(3278):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-22 08:29:37.038: E/AndroidRuntime(3278):     ... 25 more

2 个答案:

答案 0 :(得分:2)

通常,线索在logcat中,这次甚至是明确的:

01-22 08:29:37.038: E/AndroidRuntime(3278): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4132500 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

正如docs和logcat中的错误所说,添加:

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

在AndroidManifest中的<application>元素下,例如:

<application
      android:name=".OTPApp"  
      android:icon="@drawable/ic_launcher_opentripplanner"
      android:label="@string/app_name"
      android:allowBackup="true">

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

   ..rest of the manifest

答案 1 :(得分:2)

在我的清单文件中添加这两行之后就可以了。

在Application标签下添加此项 对于Eclipse:

<meta-data android:name="com.google.android.gms.version" 
 android:value="@integer/google_play_services_version" />

<meta-data android:name="com.google.android.maps.v2.API_KEY"
          android:value="Your KEY" />

如果您使用的是带有gradle0.7 +的Android Studio,请添加以下行:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your Key" />