谷歌地图Android API v2每次都会崩溃



在我的设备上运行时总是崩溃,我尝试了很多东西,但唉,我是android的初学者"地点"部分只会显示你在地图上的位置和你的坐标,对我来说似乎根本不起作用。

这是我的舱单代码

按建议编辑代码后仍然不工作,这里的日志目录

日志猫

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。删除这一行:

<uses-library android:name="com.google.android.maps" />

从清单文件,它是为谷歌地图API V1。

2。将这段代码置于应用程序标签的底部:

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="My API CODE"/>

为Android API V2的谷歌地图生成的密钥。

3。权限中的包名:

 <permission 
    android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature"></permission>
<uses-permission 
    android:name="com.example.androidmapsv2.permission.MAPS_RECEIVE"/>

与为应用程序指定的包名不匹配(它应该):

package="com.hangoverhelper"

相关内容

  • 没有找到相关文章

最新更新