0

Trying to learn how to use the google maps api and I was following a tutorial on youtube. When I run the Given Code, then app closes & a Massage "... has stopped responding" Shows Up. Help Please.

Below is the code:

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

    private GoogleMap mainMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpMap();    //line 27

    }

    private void setUpMap() {

        if(mainMap == null) {
            mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();    //line 34      
            if(mainMap != null) {
                startMap();
            }
        }

    }

    private void startMap() {       

        mainMap.setMyLocationEnabled(true);
        mainMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();     //retrieve provider
        String provider = locationManager.getBestProvider(criteria, true);      //name of best provider
        Location myLocation = locationManager.getLastKnownLocation(provider);           

        double myLat = myLocation.getLatitude();
        double myLong = myLocation.getLongitude();

        LatLng latLng = new LatLng(myLat, myLong);

        mainMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mainMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        mainMap.addMarker(new MarkerOptions().position(new LatLng(myLat, myLong)).title("You are here"));

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {           
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

}

Here is the logcat:

01-18 12:16:20.915: E/AndroidRuntime(5163): FATAL EXCEPTION: main
01-18 12:16:20.915: E/AndroidRuntime(5163): Process: com.example.map, PID: 5163
01-18 12:16:20.915: E/AndroidRuntime(5163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.map/com.example.map.MainActivity}: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.access$800(ActivityThread.java:142)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.os.Looper.loop(Looper.java:136)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.main(ActivityThread.java:5118)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at java.lang.reflect.Method.invoke(Method.java:515)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at dalvik.system.NativeStart.main(Native Method)
01-18 12:16:20.915: E/AndroidRuntime(5163): Caused by: java.lang.NullPointerException
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.example.map.MainActivity.setUpMap(MainActivity.java:34)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at com.example.map.MainActivity.onCreate(MainActivity.java:27)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.Activity.performCreate(Activity.java:5275)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-18 12:16:20.915: E/AndroidRuntime(5163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-18 12:16:20.915: E/AndroidRuntime(5163):     ... 11 more

here is the layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout> 
  • Examine LogCat to see the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this If you do not understand the stack trace, post the **entire** stack trace. – CommonsWare Jan 18 '15 at 17:21
  • Thanks, added the entire logcat. – Askskd Rieid Jan 18 '15 at 17:22
  • No, you did not. The link in my earlier comment shows you how to see LogCat; that question and answer, in turn, link to [another Stack Overflow question and answer](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) about what a Java stack trace is and what it looks like. – CommonsWare Jan 18 '15 at 17:23
  • sorry for being a noob apologize for not reading - doing that now - excuse the english please – Askskd Rieid Jan 18 '15 at 17:25
  • Ah, much better! Also, it was good of you to label which lines in the source match the lines referenced in the stack trace. – CommonsWare Jan 18 '15 at 17:31
  • If you are at it already you can also include your layout.xml – Murat Karagöz Jan 18 '15 at 17:32
  • thanks sir. i will added the layout – Askskd Rieid Jan 18 '15 at 17:39

3 Answers3

1

Either:

  • getSupportFragmentManager() is returning null, which is unlikely, or

  • findFragmentById() is returning null

The latter might occur if there is no fragment named R.id.map in the res/layout/activity_main.xml file.

UPDATE

Your layout is using com.google.android.gms.maps.MapFragment. That will not work with FragmentActivity. Use com.google.android.gms.maps.SupportMapFragment if you are going to use FragmentActivity and the fragment backport.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You are trying to use the method getMap() which was not iniialized in the first place. Try it without it.

mainMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));    //line 34
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

Since you setup your map with onCreate. I can see any number of things causing the app to not show, then Android later reports it's stopped responding etc etc.

Try throwing your setUpMap() call in onStart instead, wrap it in a Try Catch and see what happens. For better performance put that call on it's own thread. That way it doesn't bog down the start up time.

@Override
protected void onStart() {
    setUpMap();
}

private void setUpMap() {
    try {
        // your code here
    } catch (Exception e) {

    }
}
David Carrigan
  • 751
  • 1
  • 8
  • 21