android - 打开地图会让应用程序崩溃

时间:2014-01-20 19:04:16

标签: android map fragment

我有一个应用程序,当我点击图像时打开地图。我对活动的布局做了一些更改,地图停止工作。我不知道错误是什么,我没有改变地图类,清单也应该没问题。

这是我的setOnClickListner:

        dove.setOnClickListener(new OnClickListener() { 
        @Override
        public void onClick(View view) {
            conn=isOnline();
            if (conn==true){
                Toast.makeText(contatti.this,"Connessione dati PRESENTE...", Toast.LENGTH_SHORT)
                .show();

            final ProgressDialog pd = ProgressDialog.show(contatti.this, "", "Attendere...", true);
            new Thread(new Runnable(){
                 public void run(){
                     Intent intent = new Intent(contatti.this, Dove_Siamo.class);
                     startActivity(intent);
                     pd.dismiss();
                    }
               }).start();
            } else {
                new AlertDialog.Builder(contatti.this)
                .setTitle("ATTENZIONE!!!")
                .setMessage("Connessione dati assente. Non è possibile proseguire!!!")
                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { 
                        //do nothing
                        dialog.cancel();
                    }
                })
                .setIcon(R.drawable.ic_andro)
                .show();
            }
        } 
    });

和logcat:

01-20 20:02:29.548: E/AndroidRuntime(24562): FATAL EXCEPTION: main
01-20 20:02:29.548: E/AndroidRuntime(24562): java.lang.RuntimeException: Unable to start activity ComponentInfo{it.stepapp.bajabeach/it.stepapp.bajabeach.Dove_Siamo}: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.os.Looper.loop(Looper.java:137)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread.main(ActivityThread.java:4856)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at java.lang.reflect.Method.invoke(Method.java:511)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at dalvik.system.NativeStart.main(Native Method)
01-20 20:02:29.548: E/AndroidRuntime(24562): Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class fragment
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:133)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:261)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at it.stepapp.bajabeach.Dove_Siamo.onCreate(Dove_Siamo.java:77)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.Activity.performCreate(Activity.java:5047)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
01-20 20:02:29.548: E/AndroidRuntime(24562):    ... 11 more
01-20 20:02:29.548: E/AndroidRuntime(24562): 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-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.SupportMapFragment$b.eb(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.dynamic.a.a(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:269)
01-20 20:02:29.548: E/AndroidRuntime(24562):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-20 20:02:29.548: E/AndroidRuntime(24562):    ... 23 more
01-20 20:02:29.588: E/android.os.Debug(24370): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

关于如何解决这个问题的任何想法?

编辑:OnCreate代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dove_siamo);

                //sherlock actionbar code
        Drawable dab=getResources().getDrawable(R.drawable.frontespizio);  
        ActionBar ab;
        ab = getSupportActionBar();

        Drawable b=getResources().getDrawable(R.drawable.sfondoab);
        ab.setSplitBackgroundDrawable( b );


        if (ab.isShowing()) {
            ab.setBackgroundDrawable(dab);
            ab.setDisplayShowHomeEnabled(false);
            ab.setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
            getSupportActionBar().setCustomView(R.layout.abs_layout);
            ImageButton info=(ImageButton) findViewById(R.id.bottone1);
            info.setOnClickListener(new OnClickListener() { 
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Dove_Siamo.this, Stepapp_info.class);
                    startActivity(intent);
                } 
               });
        }

        current=null;
        location=null;
        test = (TextView) findViewById(R.id.tv_info);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            provs=locationManager.getProviders(true);

            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            if (location != null) {
                mlat = (double) location.getLatitude();
                mlng = (double) location.getLongitude();
                KIEL = new LatLng(mlat, mlng);
            }else {
                mlat = (double) 53.551;
                mlng = (double) 9.993;
                KIEL = new LatLng(mlat, mlng);
            }
            geocoder = new Geocoder(Dove_Siamo.this);
            mIndirizzi = geocoder.getFromLocationName(indirizzo, 1);
            mappato = mIndirizzi.get(0);
            lat = (double) (mappato.getLatitude());
            lng = (double) (mappato.getLongitude());
            HAMBURG = new LatLng(lat, lng);
            locFound=1;
        } catch (IOException e) {
            lat = 41.857512;
            lng = 12.502932;
            HAMBURG = new LatLng(lat, lng);
            e.printStackTrace();
        }

        //######## per sdk7
        FragmentManager fragmentManager = getSupportFragmentManager();
        SupportMapFragment mapFragment =  (SupportMapFragment)
                fragmentManager.findFragmentById(R.id.map);
        map=mapFragment.getMap();
        //#######


        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
        hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title(indirizzo));

        kiel = map.addMarker(new MarkerOptions()
            .position(KIEL)
            .title("Tu sei qui")
            .snippet("enjoy")
            .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_andro)));

        String url = "http://maps.googleapis.com/maps/api/directions/json?origin="
                + mlat
                + ","
                + mlng
                + "&destination="
                + lat
                + "," + lng + "&sensor=false";

        Log.i("Dove_Siamo",url);

        JSONDrivingDirectionsParser parser = new JSONDrivingDirectionsParser(url);
        punti = parser.getPath();

        info = parser.getInfo();
        test = (TextView) findViewById(R.id.tv_info);
        test.setText(info);
        String distanza="Sei a "+info;
        test.setText(distanza);

        for(int i = 0; i<punti.size()-1;i++){
            GeoPoint src1 = punti.get(i);
            GeoPoint dst1 = punti.get(i+1);
            double lts = (double) (src1.getLatitudeE6()) / 1E5;
            double lngs = (double) (src1.getLongitudeE6()) / 1E5;
            LatLng src= new LatLng(lts,lngs);
            double ltd = (double) (dst1.getLatitudeE6()) / 1E5;
            double lngd = (double) (dst1.getLongitudeE6()) / 1E5;
            LatLng dest= new LatLng(ltd,lngd);

            Polyline line = map.addPolyline(new PolylineOptions()
            .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude,   dest.longitude))
            .width(4)
            .color(Color.BLUE).geodesic(true));
        }

    }

3 个答案:

答案 0 :(得分:1)

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

对于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" />

答案 1 :(得分:0)

从这条线来判断:

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

我会验证Google Play服务库项目是否在我的Java构建路径上,因为在我看来,找不到具有正确值的R类。

答案 2 :(得分:0)

<强>解决! 检查以下内容。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR PACKAGE"
    >
<permission
        android:name="YOUR PACKAGE.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name=" YOUR PACKAGE.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
….
>


       <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 API KEY " />
    </application>
<!--In layout declaring.-->

<fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {
    private GoogleMap map;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        map = ( (SupportMapFragment) getSupportFragmentManager( ).findFragmentById( R.id.map ) ).getMap( );
    }
}

这应该有效:) Happy Coding Google Maps:)