views:

374

answers:

5

Hi all.

Going round in circles here i think.

I have an activity called Locate;

public class Locate extends Activity {

public static String lat;
public static String lon;
public static String number;

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

    final Button buttonMaps = (Button) findViewById(R.id.ButtonMaps);
    buttonMaps.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         Toast.makeText(getBaseContext(), "Button Pressed", Toast.LENGTH_SHORT).show();


        try {
         Intent i = new Intent(getBaseContext(), displayMap.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(i);
         }
         catch (ActivityNotFoundException e) {
          Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_SHORT).show(); 
         }



    }});

// Toast.makeText(getBaseContext(), "lat: " + lat + " long: " + lon + " from: " + testname, Toast.LENGTH_LONG).show();
}

If I make the displayMap class into a normal Activity, and just have display a toast message confirming it has loaded - then it works fine.

If i do this though;

public class displayMap extends MapActivity 
{    
/** Called when the activity is first created. */
public void onCreate()
{

    setContentView(R.layout.displaymap);
    Toast.makeText(getBaseContext(), "Display Map", Toast.LENGTH_SHORT).show();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

Then as soon as I click the Button, I get a force close.

I have the correct 'uses-library' tag in my manifest;

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<uses-library android:name="com.google.android.maps" />

I don't get what it just force closes everytime i try and load it.

If I make this my onClick handler then it will fire up a working googlemaps/default mapview

        public void onClick(View v) {
         Toast.makeText(getBaseContext(), "Button Pressed", Toast.LENGTH_SHORT).show();

         Uri uri=Uri.parse("geo:"+Locate.lat+","+Locate.lon);
         StartActivity(new Intent(Intent.ACTION_VIEW, uri));
         }

But that is not what I am trying to do, I want my own - so that I can add overlays etc to it. But it does prove that the permission are set correctly and that the lib is there.

The logcat error when the app FCs is a Unexpected DEX error.

Can anyone point in the right direction here?

+1  A: 

In your onCreate in your displayMap, you forgot to call the super class

public class displayMap extends MapActivity 
{    
/** Called when the activity is first created. */
public void onCreate()
{
    super.onCreate(icicle); /************* Line to add ********/
    setContentView(R.layout.displaymap);
    Toast.makeText(getBaseContext(), "Display Map", Toast.LENGTH_SHORT).show();
}

Also getBaseContext() is not recommended - Use getContext() or this

More in this stackoverflow post

ccheneson
even with that done, it still force closes. I will add more info below.
arc
A: 

Ok, i found the problem. For some reason I had google apis, and then the maps.jar listed on my buildpath / lib in eclipse. Cleaned it up and it's working as expected now.

arc
A: 

Hi, I am trying to create a similar application as yours (showing MapView in response to button press). having the same problem as you had. What did you write in your manifest file for the displayMap activity?

I have as follows:

Please suggest, what I might have done wrong.

Thanks. Fahin

Fahim
A: 

Hi, I am trying to create a similar application as yours (showing MapView in response to button press). having the same problem as you had. What did you write (for intent-filter) in your manifest file for the displayMap activity?

I have as follows:

<activity android:name=".MyMapViewer" android:label="@string/my_map_viewer">
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.SAMPLE_CODE" /> </intent-filter> </activity>

Fahim
there was nothing wrong with my intent filter, i was having issues with the project build info. Re your manifest, have you added this line; <uses-library android:name="com.google.android.maps" /> it needs to be a child element of application. You will also need <uses-permission android:name=”android.permission.INTERNET”>.Suggest you get this tutorial working, and then develop from there.http://developer.android.com/guide/tutorials/views/hello-mapview.html
arc
A: 

ok, thanks a lot, I solved it. I also had build/configuration issue.

Fahim