2016-12-29 72 views
-2

我已經閱讀了許多關於同一問題的其他答案,但這一個是不同的。NullPointerException在獲取地圖片段

我的應用程序包含一個活動,顯示我在地圖中的當前位置。我的主要活動不是顯示地圖的。

下面是該活性(地圖顯示活性)的代碼:

public class ActivityShowsNearby extends BaseActivity implements GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener, 
     OnMapReadyCallback { 

    private static final String TAG = "ActivityShowsNearby"; 
    private static GoogleApiClient mGoogleApiClient; 
    private Location mCurrentLocation; 
    private LocationRequest mLocationRequest; 


    private static final int LOCATION_REQUEST_INTERVAL = 1000 * 60; //60 seconds 
    private static final int LOCATION_REQUEST_FASTEST_INTERVAL = 1000 * 60; //60 seconds 


    // Request codes... 


    private GoogleMap mGoogleMap; 
    private static final int DEFAULT_ZOOM = 15; 
    private final LatLng mDefaultLocation = new LatLng(28.636424, 77.374493); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     Log.v(TAG, "onCreate"); 
     super.onCreate(savedInstanceState); 
     super.setContentView(R.layout.activity_shows_nearby); 

     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API) 
        .build(); 
     } 

    } 


    @Override 
    protected void onStart() { 
     Log.v(TAG, "onStart"); 

     mGoogleApiClient.connect(); 
     super.onStart(); 


    } 

    @Override 
    protected void onResume() { 
     Log.v(TAG, "onResume"); 

     if (mGoogleApiClient.isConnected()) { 
      Log.v(TAG, "Starting location updates..."); 

      startLocationUpdates(); 
     } 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     Log.v(TAG, "onPause"); 
     if (mGoogleApiClient.isConnected()) { 
      Log.v(TAG, "Removing location updates..."); 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
     super.onPause(); 
    } 


    @Override 
    protected void onStop() { 
     Log.v(TAG, "onStop"); 

     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 


    @Override 
    public void onConnected(Bundle bundle) { 
     startLocationUpdates(); 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 

     if (mapFragment == null) { 
      Log.v(TAG, "mapFragment is null"); 
     } else { 
      Log.v(TAG, "mapFragment is NOT null"); 

     } 
     mapFragment.getMapAsync(this); 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.v(TAG, "mGoogleApiClient connection suspended"); 


    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.v(TAG, "mGoogleApiClient connection failed"); 


    } 

    private void createLocationRequest() { 
     if (mLocationRequest == null) { 
      mLocationRequest = new LocationRequest(); 
      mLocationRequest.setInterval(LOCATION_REQUEST_INTERVAL); 
      mLocationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST_INTERVAL); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     } 

    } 

    private void startLocationUpdates() { 
     createLocationRequest(); 
     try { 
      LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, 
        mLocationRequest, 
        this 
      ); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     mCurrentLocation = location; 
     updateMap(); 
    } 



    //@SuppressWarnings("MissingPermission") 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 


     Log.v(TAG, "onMapReady"); 
     mGoogleMap = googleMap; 

     updateLocationUI(); 

     if (mCurrentLocation != null) { 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(mCurrentLocation.getLatitude(), 
          mCurrentLocation.getLongitude()), DEFAULT_ZOOM)); 
     } else { 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM)); 
     } 

    } 

    private void updateMap() { 
     if (mGoogleMap == null) { 
      return; 
     } 
     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
       new LatLng(mCurrentLocation.getLatitude(), 
         mCurrentLocation.getLongitude()), DEFAULT_ZOOM)); 
    } 


    @SuppressWarnings("MissingPermission") 
    private void updateLocationUI() { 

     if (mGoogleMap == null) { 
      Log.v(TAG, "mGoogleMap is null"); 
      return; 
     } 

     mGoogleMap.setMyLocationEnabled(true); 
     mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true); 


    } 

} 

顯然,這種活動從中我已創建以顯示導航抽屜BaseActivity.java延伸。這裏是BaseActivity.java

public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ 

    private static final String TAG = "BaseActivity"; 
    private Toolbar toolbar; 
    private DrawerLayout drawerLayout; 
    private ActionBarDrawerToggle actionBarDrawerToggle; 
    private NavigationView navigationView; 

    //These two values will be used in unison -- see their usage 
    private boolean mDrawerItemClicked; 
    private int mDrawerItemSelectedId; 


    public NavigationView getNavigationView() { 
     return navigationView; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, "onCreate"); 

     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public void setContentView(int layoutResID) { 
     Log.d(TAG, "setContentView"); 

     super.setContentView(layoutResID); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
    } 

    //Other navigation drawer set up codes... 
} 

當應用程序第一次運行的代碼,我的主要活動出現。然後用戶可以打開導航抽屜並選擇一個導航抽屜項目來打開我的地圖展示活動,例如ActivityShowsNearby,如下圖所示。

Here is the screenshot of the same

這是問題所在。

從我的主要活動中,我打開導航抽屜並選擇show near me打開ActivityShowsNearby。然後我再次按回去主要活動。一切工作正常到這裏。但現在,如果我打開導航抽屜,並再次選擇show near me重新打開ActivityShowsNearby活動,然後應用程序崩潰。

這裏是logcat的三個場合:

擊中SHOW NEAR ME第一次。

12-29 16:30:53.696 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onCreate 
12-29 16:30:54.386 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onStart 
12-29 16:30:54.416 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onResume 
12-29 16:30:54.416 8585-8585/sumit.example.com.movie D/InputTransport: Input channel constructed: name='d9453f5 sumit.example.com.movie/com.example.sumit.movie.ActivityShowsNearby (client)', fd=56 
12-29 16:30:54.596 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: Connected to Google play services. 
12-29 16:30:54.636 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: mapFragment is NOT null 
12-29 16:30:54.786 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onMapReady 

後,我按從我ActivityShowsNearby回:

12-29 16:31:02.576 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onPause 
12-29 16:31:02.576 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: Removing location updates... 
12-29 16:31:02.986 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onStop 
12-29 16:31:02.996 8585-8585/sumit.example.com.movie D/InputTransport: Input channel destroyed: name='d9453f5 sumit.example.com.movie/com.example.sumit.movie.ActivityShowsNearby (client)', fd=56 

最後,當我打開抽屜式導航欄再次點擊show near me

12-29 16:31:07.856 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onCreate 
12-29 16:31:07.906 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onStart 
12-29 16:31:07.906 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: onResume 
12-29 16:31:07.906 8585-8585/sumit.example.com.movie D/InputTransport: Input channel constructed: name='d32ef5a sumit.example.com.movie/com.example.sumit.movie.ActivityShowsNearby (client)', fd=82 
12-29 16:31:08.016 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: Connected to Google play services. 
12-29 16:31:08.016 8585-8585/sumit.example.com.movie V/ActivityShowsNearby: mapFragment is null 
12-29 16:31:08.026 8585-8585/sumit.example.com.movie E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: sumit.example.com.movie, PID: 8585 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference 
                      at com.example.sumit.movie.ActivityShowsNearby.onConnected(ActivityShowsNearby.java:168) 
                      at com.google.android.gms.common.internal.zzm.zzq(Unknown Source) 
                      at com.google.android.gms.internal.zzaal.zzo(Unknown Source) 
                      at com.google.android.gms.internal.zzaaj.zzvE(Unknown Source) 
                      at com.google.android.gms.internal.zzaaj.onConnected(Unknown Source) 
                      at com.google.android.gms.internal.zzaan.onConnected(Unknown Source) 
                      at com.google.android.gms.internal.zzzy.onConnected(Unknown Source) 
                      at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source) 
                      at com.google.android.gms.common.internal.zzf$zzj.zzwZ(Unknown Source) 
                      at com.google.android.gms.common.internal.zzf$zza.zzc(Unknown Source) 
                      at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source) 
                      at com.google.android.gms.common.internal.zzf$zze.zzxa(Unknown Source) 
                      at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:7406) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

有什麼問題嗎?

請假設我已啓用位置權限和設置。

這裏是我的activity_show_nearby.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:orientation="vertical" 
     tools:context="com.example.sumit.movie.MovieActivity"> 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      android:elevation="4dp" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

     <!-- Start of replacement of fragment-container--> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:id="@+id/activity_shows_nearby_location_layout"> 

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


     </LinearLayout> 

     <!--End of replacement of fragment container--> 


    </LinearLayout> 


    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_movie" 
     app:menu="@menu/activity_movie_drawer" /> 

</android.support.v4.widget.DrawerLayout> 
+0

請發佈'activity_shows_nearby.xml'文件 –

+0

補充,請 現在檢查。 –

+0

提供[mcve]。這似乎是一個片段事務的簡單問題,但這裏有許多無用信息的方法 – AxelH

回答

-1

添加class屬性,而不是android:name

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

檢查更多信息MapFragment

+0

仍然崩潰.. –

+0

清潔項目,然後再試一次。 –

-1

你的日誌顯示片段對象爲null:

V/ActivityShowsNearby: mapFragment is null 

所以你試圖調用一個方法(getMapAsync),它會導致錯誤。

另外,還要確保你將覆蓋所有的生命週期方法(發現的onDestroy丟失)

@Override 
public void onDestroy(){ 
    super.onDestroy(); 
} 
+2

重寫僅用於調用超類聲明的方法是毫無用處的。我相信OP要求瞭解它爲什麼只有在重新打開該活動後纔有NULL,因爲有一個LOG來檢查它是否爲NULL;) – AxelH

0

想你的代碼除非菜單初始化

app:headerLayout="@layout/nav_header_movie" 
app:menu="@menu/activity_movie_drawer" 

片段不爲空(日誌記錄:mapFragment不null) 嘗試清理並重新編譯您的項目

相關問題