1

每次我試圖測試我的手機上我的應用程序,該應用程序快速打開,顯示一個白色的屏幕,然後後立即關閉。然後顯示提示「不幸的是,應用程序已停止」。應用程序中使用上啓動Android谷歌地圖V2崩潰

我已經檢查的構建路徑,我確信,谷歌播放服務添加庫。我試着就如何解決已經在搜索多個線程和他們都不工作。所以我把這個帖子作爲讓別人手動檢查它的最後手段。

這裏是我的代碼:

package com.example.vendsale; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.content.IntentSender; 
import android.database.Cursor; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.CircleOptions; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MainActivity extends FragmentActivity implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener 

{ 
    private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
    LocationClient mNewLocationClient; 
    private GoogleMap map; 
    private LatLng userLocation; 
    private DatabaseConnector dbConnector; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     checkGooglePlayServicesAvailability(); 

     dbConnector = new DatabaseConnector(getApplicationContext()); 
     map = ((MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map)).getMap(); 
     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     map.getUiSettings(); 
     map.setMyLocationEnabled(true); 

     mNewLocationClient = new LocationClient(this, this, this); 

    } 

    protected void onStart() { 
     super.onStart(); 

     mNewLocationClient.connect(); 

     userLocation = new LatLng(mNewLocationClient.getLastLocation() 
       .getLatitude(), mNewLocationClient.getLastLocation() 
       .getLongitude()); 
     CameraUpdate update = CameraUpdateFactory.newLatLng(userLocation); 
     map.animateCamera(update); 

    } 

清單:

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

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

    <permission 
     android:name="com.example.vendsale.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

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

    <uses-permission 
     android:name="com.example.vendsale.permission.MAPS_RECEIVE" 
     /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <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="com.example.vendsale.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="MY KEY" /> 
    </application> 

</manifest> 

活動主營:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    style="@style/AppBaseTheme" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="left" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignRight="@+id/map" 
     android:onClick="onClick_Search" 
     android:text="@string/ButtonText" 
     android:typeface="normal" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:gravity="left" 
     android:hint="@string/message" 
     android:inputType="text" 
     android:typeface="normal" 
     android:layout_toLeftOf="@id/button1" /> 

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



</RelativeLayout> 
+1

logcat的輸出... –

回答

0

你已經在你的XML使用的supportmapfragment類。

添加此導入進口com.google.android.gms.maps.SupportMapFragment;

和更改地圖intialisation路線爲:

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

我假設在你的代碼的mNewLocationClient部分工作正常,並且沒有nullpointers那裏。

相關問題