Google maps fails to open in second activity

我与影子孤独终老i 提交于 2020-04-30 04:54:23

问题


I'm making an application and ran into a problem.
I made separately app for google maps and it worked perfect, and I made second app in which I have listview, and by clicking on item in listview, another activity starts where google maps with different marks opens.
So I manage to get another activity running after clicking on separate objects in listview, so I modified that objects .xml and .java file on same principle as I did in my google maps app but somehow it doesn't works.

Main_Activity

    package com.example.e_idem;

    import android.os.Bundle;
    import android.app.AlertDialog;
    import android.app.ListActivity;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
        import android.widget.TextView;
        import android.widget.Toast;

    public class MainActivity extends ListActivity {
    String [] usluga = {
        "Frizerski salon",
        "Kafić",
        "Vodoinstalater",
        "Bravar",
        "Ljekarna",
        "Pekara",
        "Autoservis",
        "Dućan",
        "Knjižnica",
        "Papirnica",
        "Željezara",
        "Smartshop",
        "Servis bicikla"
        };
            @Override
            protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Odaberite uslugu koju tražite.")
               .setCancelable(false)
               .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        }
               });
        AlertDialog alert = builder.create();
        alert.show();
        TextView messageView = (TextView)alert.findViewById(android.R.id.message);
        messageView.setGravity(Gravity.CENTER);


        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usluga));


    }
    public void onListItemClick(ListView parent, View view, int position,long id)
    {
        Toast.makeText(this, "Odabrali ste uslugu : "+usluga[position], Toast.LENGTH_LONG).show();

        switch( position )
        {
           case 0:  Intent activity_frizerski_salon = new Intent(this, FrizerskiSalon.class);     
                    startActivity(activity_frizerski_salon);
                    break;
           case 1:  Intent activity_kafic = new Intent(this, Kafic.class);     
                    startActivity(activity_kafic);
                    break;
           case 2:  Intent activity_vodoinstalater= new Intent(this, Vodoinstalater.class);     
                    startActivity(activity_vodoinstalater);
                    break;

        }
    }


}

Activity I call in Main_Activity for clicking an object in listview

 package com.example.e_idem;

    import com.google.android.gms.maps.*;
    import com.google.android.gms.maps.model.*;
    import android.app.Activity;
    import android.app.ListActivity;
    import android.os.Bundle;

    public class FrizerskiSalon extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frizerski_salon);

        // Get a handle to the Map Fragment
        GoogleMap map = ((MapFragment) getFragmentManager()
                .findFragmentById(R.id.fmap)).getMap();

        LatLng vz = new LatLng(46.305746, 16.336606);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(vz, 13));

        map.addMarker(new MarkerOptions()
                .title("Varaždin")
                .snippet("Barokni grad")
                .position(vz));

        map.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag_red))
        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
        .position(new LatLng(46.300274, 16.330769)));

        map.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag_red))
        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
        .position(new LatLng(46.306274, 16.300769)));

        map.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag_red))
        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
        .position(new LatLng(46.306974, 16.303169)));

        map.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag_red))
        .anchor(0.0f, 1.0f) // Anchors the marker on the bottom left
        .position(new LatLng(46.302874, 16.400710)));
    }
}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.e_idem"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <permission
        android:name="com.example.e_idem.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.e_idem.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <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-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/house_flag"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.example.e_idem.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>

        <activity
            android:name="com.example.e_idem.FrizerskiSalon"
            android:label="@string/activity_frizerski_salon" >
        </activity>

        <activity
            android:name="com.example.e_idem.Kafic"
            android:label="@string/activity_kafic" >
        </activity>

        <activity
            android:name="com.example.e_idem.Vodoinstalater"
            android:label="@string/activity_vodoinstalater" >
        </activity>


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

</manifest>

And my LogCat http://paste.strictfp.com/39582


回答1:


you need 2 meta-data entries in your manifest file:

1) map key

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

2) google play services version

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

You are missing the second one



来源:https://stackoverflow.com/questions/20076748/google-maps-fails-to-open-in-second-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!