Android Map版本2问题

时间:2013-07-26 09:17:02

标签: android map

我是android的新手。我为MAP创建了一个应用程序,我在模拟器中安装了vending.apk和gms.apk,用于在模拟器中运行地图。还可以在我的eclipse中复制google-play-service_lib项目,并在我的项目lib文件夹中包含google-play-service.zip,并在项目中包含google-play-service.jar。在运行时它显示错误。 Logcat如下。请帮我解决这个问题。

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

        }

清单

    <?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

2 个答案:

答案 0 :(得分:0)

您需要在地图项目中引用Google Play服务库项目。

将google-play services_lib库项目复制到您的工作区(您的android地图项目所在的文件夹)。可以在以下路径中找到库项目。

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

将库项目导入您的eclipse

点击文件&gt;导入,选择Android&gt;将现有的Android代码导入Workspace,并浏览工作区导入库项目。您可以检查它是否是库项目。右键单击库项目。转到属性。点击左侧面板上的Android。您将看到Is Library已选中。

右键单击你的android项目。转到属性。在左侧面板中选择Android。单击“添加”并浏览库项目。选择相同。单击“确定”并应用

enter image description here

编辑:

你的min sdk是8所以你应该使用SupportMapFragment

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

您的活动必须延长FragmentActivity

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

同时确保导入以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;  

答案 1 :(得分:0)

我认为您已对此活动的设置布局的代码进行了评论,请在下面的行中取消注释检查一次,然后检查。

//setContentView(R.layout.activity_main);