1

I'm working on an Android app that should start a MapView when a tour is selected from a list. However The app seems to crash everytime I try to open this MapView via an Intent...

Here is my code:

MainActivity.java:

public class MainActivity extends Activity {
    public final static String SELECTED_TOUR = "com.me.tourapp.TOUR";
    public static TourListAdapter tourListAdapter;
    private static int selectedTour;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    public void startTour(View view) {
        Intent intent = new Intent(view.getContext(), TourMapActivity.class);
        intent.putExtra(SELECTED_TOUR, selectedTour);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ...
    }

}

TourMapActivity.java:

public class TourMapActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tour_map);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }

        //check if tour is passed via intent (test):
        Intent intent = getIntent();
        int tourIndex = Integer.parseInt(intent.getStringExtra(MainActivity.SELECTED_TOUR));
        Toast.makeText(getApplicationContext(), "the tour: "+ tourIndex, Toast.LENGTH_LONG).show();
    }
}

activity_tour_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

and some snippets from the manifest:

<permission
    android:name="com.me.tourapp.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="com.me.tourapp.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.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-library android:name="com.google.android.maps" />

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

I also included android-support-v4.jar to the java build path and installed and imported the google play services...

I just don't hava a clue where I'm making the errors...

edit: I added the logcat:

E/AndroidRuntime(28248): FATAL EXCEPTION: main
E/AndroidRuntime(28248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hansvn.plaktour/com.hansvn.plaktour.TourMapActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
E/AndroidRuntime(28248):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
E/AndroidRuntime(28248):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
E/AndroidRuntime(28248):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
E/AndroidRuntime(28248):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(28248):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28248):    at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(28248):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28248):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(28248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(28248):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(28248): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
E/AndroidRuntime(28248):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(28248):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
E/AndroidRuntime(28248):    at android.app.Activity.setContentView(Activity.java:1924)
E/AndroidRuntime(28248):    at com.hansvn.plaktour.TourMapActivity.onCreate(TourMapActivity.java:16)
E/AndroidRuntime(28248):    at android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(28248):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
E/AndroidRuntime(28248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
E/AndroidRuntime(28248):    ... 11 more
E/AndroidRuntime(28248): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
E/AndroidRuntime(28248):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(28248):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
E/AndroidRuntime(28248):    ... 21 more
E/AndroidRuntime(28248): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
E/AndroidRuntime(28248):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime(28248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(28248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(28248):    at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
E/AndroidRuntime(28248):    ... 24 more
Hans Vn
  • 787
  • 1
  • 16
  • 32

1 Answers1

1

You messing API V2 with API V1 of Google Maps. So first of all decide which API are you going to use.

Next, if you have decided that you are using API V2 (as it looks by most of your code), then remove this line:

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

It's part of API V1 and doesn't need to be in V2.

Second thing you need to decide is what is the minAPI level of your application. If you are writing your application to API level < 11 then you should use SupportMapFragment in your XML, like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

and your activity should extend FragmentActivity, this is the case were you should use the google-support-v4 package, because FragmentActivity is part of this package.

UPDATE: From your logcat output:

 Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.me.tourapp/com.me.tourapp.TourMapActivity}; have you declared this activity in your AndroidManifest.xml?

which means you have not declared your Activity TourMapActivity in the manifest file, and this is the reason for your error.

UPDATE2: this error:

Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public.

derived as said in the error from the fact that SupportMapFragment class was not found. what means that you haven't reference the google-play-services library the right way because this class is part of this library. please read the first 3 steps of this blog post I wrote on how to reference it correctly:

Google Map API V2

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • I tried your suggestions, but it still doesn't work... I removed the tag from the manifest, since I'm using V2, I updated the fragment and extended the class with FragmentActivity... – Hans Vn Apr 23 '13 at 16:21
  • From the logcat you posted you have not declared your Activity TourMapActivity in the manifest file, and this is the reason for your error – Emil Adz Apr 23 '13 at 16:33
  • meanwhile I added the Activity to the manifest file, thought eclipse did that automatically :s but still no joy... I updated the logcat with the errors I'm getting now – Hans Vn Apr 23 '13 at 16:44
  • thanks so far! :) now I get another error:E/Google Maps Android API(29122): Failed to load map. Could not contact Google servers... I have an api key generated from Google's APIs Console... – Hans Vn Apr 23 '13 at 17:10
  • if this is the error then there is some thing wrong with your key or the way you registered it in the console, I strongly recommend you to go over this post I wrote: http://blog-emildesign.rhcloud.com/?p=403 and you are making all the step right. if this doesn't help you can try to delete the debug.keystore folder and compile any project in eclipse to regenerate it, and then try to register your application in the console with the new SHA1 key you will recieve. – Emil Adz Apr 23 '13 at 17:19
  • Found it! seems I activated the wrong service at the API Console... http://stackoverflow.com/questions/13803833/map-api-v2-authorisation-failure/13805807#13805807 – Hans Vn Apr 23 '13 at 17:44