2016-12-29 12 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... 
} 

のコードは、私の主な活動は表示されます。次に、ユーザーはナビゲーションドロワーを開き、ナビゲーションドロワー項目の1つを選択して地図表示アクティビティーを開きます(例:ActivityShowsNearby)。

Here is the screenshot of the same

ここに問題があります。

私の主な活動である、私はナビゲーションドロワーを開き、show near meを選択してActivityShowsNearbyを開きます。それから私はメインの活動に再び行くために戻る。すべてここまでうまくいきます。しかし、今度は、ナビゲーションドロワーを開いて、アクティビティを再び開くためにshow near meを再度選択すると、アプリケーションがクラッシュします。

SHOW NEAR ME初めて打つ:ここ

はlogcat 3回です。

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) 

をクリックしてここでの問題は何ですか?

ロケーション権限と設定の両方を有効にしていることを前提としています。ここで

は、より多くの情報のための代わりに android:name

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

チェックの私の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属性であるMapFragment

+0

まだクラッシュします.. –

+0

プロジェクトをクリーンアップし、もう一度やり直してください。 –

-1

あなたのログには、フラグメントオブジェクトがnullであることを示しています

V/ActivityShowsNearby: mapFragment is null 

したがって、エラーが発生するメソッド(getMapAsync)を呼び出そうとしています。また

あなたはすべてのライフサイクルメソッドをオーバーライドしていることを確認してください

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

フラグメントがnullでない

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

スーパークラス宣言を呼び出すメソッドをオーバーライドするのは無意味です。私はOPがNULLであるかどうかをチェックするLOGがあるので、OPがアクティビティを再オープンした後にのみNULLである理由を理解するよう頼みます。 – AxelH

0

は、メニューの初期化を除いて、あなたのコードを試してみました(行方不明onDestroy気づい)(レコードをLOG:mapFragmentではありませんnull) プロジェクトのクリーンアップと再コンパイルを試行してください

関連する問題