0

I am new in android.I create an application for MAP and i install vending.apk and gms.apk in emulator for running map in emulator. And also copy google-play-service_lib project in my eclipse and include google-play-service.zip in my project lib folder and include google-play-service.jar in project.In the time of running it shows error. Logcat is given below. Please help me to solve this problem.

public class MainActivity extends Activity{

            GMapV2GetRouteDirection  routedirection;
            GoogleMap googlemap;
            MarkerOptions markerOption;
            LatLng PositionFrom;
            LatLng PositionTo;
            Document document;
             MarkerOptions markerOptions;
             Location location ;
            /*static final LatLng Poprad = new LatLng(49.055, 20.299);  
            static final LatLng Prag = new LatLng(50.085, 14.458);*/
            private GoogleMap map;
            @SuppressLint("NewApi")
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //setContentView(R.layout.activity_main);
                routedirection=new GMapV2GetRouteDirection();
                /*SupportMapFragment mapfragment=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                googlemap=mapfragment.getMap();*/
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

                googlemap.setMyLocationEnabled(true);
                googlemap.getUiSettings().setZoomControlsEnabled(true);
                googlemap.getUiSettings().setCompassEnabled(true);
                googlemap.getUiSettings().setMyLocationButtonEnabled(true);
                googlemap.getUiSettings().setAllGesturesEnabled(true);
                googlemap.setTrafficEnabled(true);
                googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
                markerOption=new MarkerOptions();
                PositionFrom=new LatLng(6777.7,8888.5);
                PositionTo=new LatLng(4444.3, 3333.4);
                GetRoutTask getRoute =new GetRoutTask();
                getRoute.execute();
            }

            private class GetRoutTask extends AsyncTask<String,Void,String>{

                private ProgressDialog pdialog;
                String response = "";

                protected void onPreExecute(){
                    pdialog=new ProgressDialog(MainActivity.this);
                    pdialog.setMessage("Loading.....");
                    pdialog.show();
                }



                @Override
                protected String doInBackground(String... params) {
                    // TODO Auto-generated method stub

                document=routedirection.Getdocument(PositionFrom, PositionTo, GMapV2GetRouteDirection.MODE_DRIVING);
                response = "Success";
                return response;
                }

                  protected void onPostExecute(String result) {
                      googlemap.clear();
                      if(response.equalsIgnoreCase("Success")){
                      ArrayList<LatLng> directionPoint = routedirection.getDirection(document);
                      PolylineOptions rectLine = new PolylineOptions().width(10).color(
                                  Color.RED);

                      for (int i = 0; i < directionPoint.size(); i++) {
                            rectLine.add(directionPoint.get(i));
                      }
                      // Adding route on the map
                      googlemap.addPolyline(rectLine);
                      markerOptions.position(PositionFrom);
                      markerOptions.draggable(true);
                      googlemap.addMarker(markerOptions);

                      }

                      pdialog.dismiss();
                }



            }


            @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;
            }

        }

Manifest

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

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

        <permission
            android:name="example.map.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />

            <uses-permission android:name="example.map.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"/>
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="example.map.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>
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyBwP-HgSvM_SqUKYTdP30Jopp6V-z0LZdo"/> 
        </application>

    </manifest>

LOGCAT

07-26 14:24:22.261: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-26 14:24:22.291: E/AndroidRuntime(860): FATAL EXCEPTION: main
07-26 14:24:22.291: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.map/example.map.MainActivity}: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.os.Looper.loop(Looper.java:137)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-26 14:24:22.291: E/AndroidRuntime(860):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860):  at java.lang.reflect.Method.invoke(Method.java:511)
07-26 14:24:22.291: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-26 14:24:22.291: E/AndroidRuntime(860):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 14:24:22.291: E/AndroidRuntime(860):  at dalvik.system.NativeStart.main(Native Method)
07-26 14:24:22.291: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
07-26 14:24:22.291: E/AndroidRuntime(860):  at example.map.MainActivity.onCreate(MainActivity.java:49)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.Activity.performCreate(Activity.java:5008)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-26 14:24:22.291: E/AndroidRuntime(860):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-26 14:24:22.291: E/AndroidRuntime(860):  ... 11 more
07-26 14:25:04.591: I/Process(860): Sending signal. PID: 860 SIG: 9
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
ajay
  • 11
  • 1
  • 4

2 Answers2

0

You need to reference the google play services library project in your map project.

Copy the google-play services_lib library project to your workspace (folder where your android map project is). The library project can be found under the following path.

     <android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib library project .

Import the library project to your eclipse

Click File > Import, select Android > Existing Android Code into Workspace, and browse the workspace import the library project. You can check if it is library project. Right click on the library project. Goto properties. Click Android on the left panel. You will see Is Library checked.

Right click on your android project. Goto properties. Choose Android on the left panel. Click on Add and browse the library project. Select the same. Click ok and apply

enter image description here

Edit:

Your min sdk is 8 So you should use SupportMapFragment.

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

Your activity must extend FragmentActivity.

SupportMapFragment fm = (SupportMapFragment)  getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = fm.getMap(); 

Also make sure you imported the below

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;  
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Hi, I did that way.But that time the console shows Jar mismatch! Fix your dependencies – ajay Jul 26 '13 at 09:47
  • What is line 49 in `MainActivity.java`. Remove the jars and reference the library project as above – Raghunandan Jul 26 '13 at 09:48
  • @ajay if you do it properly i don't see any reason why it wont' work. – Raghunandan Jul 26 '13 at 10:04
  • HI, i refer that way.But that time program contains lot of errors – ajay Jul 26 '13 at 10:08
  • @ajay http://stackoverflow.com/questions/16056914/jar-mismatch-fix-your-dependencies/16056965#16056965. seems you have jars duplicated in your project. try removing them. or try as a new project. JAR mismatch says it all. Also you have not accepted any answers to the questions you asked which is not good coz i would think twice answering your future questions. – Raghunandan Jul 26 '13 at 16:44
0

i think you have comment the code for set layout for this activity, please check once by uncomment below line and check after that.

//setContentView(R.layout.activity_main);
MFP
  • 1,141
  • 9
  • 22