0

我是 android 新手。我为 MAP 创建了一个应用程序,并在模拟器中安装了 vending.apk 和 gms.apk 以在模拟器中运行地图。并且还在我的eclipse中复制google-play-service_lib项目并将google-play-service.zip包含在我的项目lib文件夹中并在项目中包含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
4

2 回答 2

0

我想你已经评论了这个活动的设置布局的代码,请通过取消注释来检查一次,然后再检查。

//setContentView(R.layout.activity_main);
于 2014-05-07T04:11:58.777 回答
0

您需要在地图项目中引用 google play services library 项目。

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

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

将库项目导入到您的 Eclipse

点击 File > Import,选择 Android > Existing Android Code into Workspace,浏览工作区导入库项目。您可以检查它是否是库项目。右键单击库项目。转到属性。单击左侧面板上的 Android。您将看到 Is Library 已选中。

右键单击您的android项目。转到属性。在左侧面板上选择 Android。单击添加并浏览库项目。选择相同。点击确定并申请

在此处输入图像描述

编辑:

你的 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;  
于 2013-07-26T09:33:00.830 回答