Google Maps APIv2实施

时间:2013-04-02 13:29:08

标签: android google-maps-android-api-2

大家好我已经尝试将以下内容实施到我的应用中。 一个页面,显示您在谷歌地图上的位置,并有以下教程,但每次我运行我的应用程序的这一部分崩溃帮助将非常感谢 谢谢

当我在我的设备上运行时它总是崩溃我已经尝试了很多东西但是我是初学者在android上 地方部分只会向您展示您在地图上的位置,而您的协调似乎根本不适合我。

这是日志猫。

Log Cat

04-02 03:45:19.025: W/dalvikvm(1102): threadid=1: thread exiting with uncaught exception     (group=0x40a71930)
04-02 03:45:19.104: E/AndroidRuntime(1102): FATAL EXCEPTION: main
04-02 03:45:19.104: E/AndroidRuntime(1102): java.lang.RuntimeException: Unable to start  activityComponentInfo{com.hangoverhelper/com.hangoverhelper.Place}: java.lang.NullPointerException
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.os.Looper.loop(Looper.java:137)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invoke(Method.java:511)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at dalvik.system.NativeStart.main(Native Method)
04-02 03:45:19.104: E/AndroidRuntime(1102):     Caused by: java.lang.NullPointerException
04-02 03:45:19.104: E/AndroidRuntime(1102):     at com.hangoverhelper.Place.onCreate(Place.java:42)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.Activity.performCreate(Activity.java:5104)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-02 03:45:19.104: E/AndroidRuntime(1102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-02 03:45:19.104: E/AndroidRuntime(1102):     ... 11 more

Place.java

package com.hangoverhelper;
import com.hangoverhelper.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.MapFragment;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Place extends Activity 
        implements LocationSource, LocationListener{

    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;
    TextView tvLocInfo;

    LocationManager myLocationManager = null;
    OnLocationChangedListener myLocationListener = null;
    Criteria myCriteria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place);
        tvLocInfo = (TextView)findViewById(R.id.locinfo);

        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment 
            = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();

        myMap.setMyLocationEnabled(true);

        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        myCriteria = new Criteria();
        myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.place, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.menu_legalnotices) {
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                    getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(Place.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);   
    }

    @Override
    protected void onResume() {
        super.onResume();

        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS){
            Toast.makeText(getApplicationContext(), 
                    "isGooglePlayServicesAvailable SUCCESS", 
                    Toast.LENGTH_LONG).show();

            //Register for location updates using a Criteria, and a callback on the specified looper thread.
            myLocationManager.requestLocationUpdates(
                    0L,             //minTime
                    0.0f,           //minDistance
                    myCriteria,     //criteria
                    this,           //listener
                    null);          //looper

            //Replaces the location source of the my-location layer.
            myMap.setLocationSource(this);

        }else{
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);    
        }

    }

    @Override
    protected void onPause() {
        myMap.setLocationSource(null);
        myLocationManager.removeUpdates(this);

        super.onPause();
    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        myLocationListener = listener;
    }

    @Override
    public void deactivate() {
        myLocationListener = null;
    }

    @Override
    public void onLocationChanged(Location location) {
        if (myLocationListener != null) {
            myLocationListener.onLocationChanged(location);

            double lat = location.getLatitude();
            double lon = location.getLongitude();

            tvLocInfo.setText(
                    "lat: " + lat + "\n" +
                    "lon: " + lon);
        }
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

的AndroidManifest.xml

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

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

    <permission 
        android:name="com.hangoverhelper.permission.MAPS_RECEIVE" 
        android:protectionLevel="signature"></permission>
    <uses-permission 
        android:name="com.hangoverhelper.permission.MAPS_RECEIVE"/>
    <uses-permission 
        android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission 
        android:name="android.permission.INTERNET"/>
    <uses-permission 
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission 
        android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission 
        android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:logo="@drawable/ic_launcher"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.hangoverhelper.SplashActivity"
            android:label="@string/title_activity_splash"
            android:theme="@style/Theme.Splash" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.hangoverhelper.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
        </activity>
        <activity
            android:name="com.hangoverhelper.Place"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/local_title"
            android:theme="@style/AppTheme" >
        </activity>
        <activity
            android:name="com.hangoverhelper.Pill"
            android:label="@string/pills_title" >
        </activity>
        <activity
            android:name="com.hangoverhelper.Food"
            android:label="@string/food_title" >
        </activity>
        <activity
            android:name="com.hangoverhelper.Coffee"
            android:label="@string/coffee_title" >
        </activity>
        <activity
            android:name="com.hangoverhelper.Home"
            android:label="@string/home_title" >
        </activity>
      <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyBkFR4cjuUf2rw8MfLBrWS8iaS2-Th5XA4"/>
    </application>

</manifest>

activity_place.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Place" >

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

    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#A0FFFFFF" >
        <TextView
            android:id="@+id/locinfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/local" />

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您的地图活动应该通过 public class Place extends Activity

扩展FragmentActivity而非活动替换public class Place extends FragmentActivity