2016-08-03 64 views
11

我想在用戶打開Goog​​le地圖活動後立即顯示快餐欄,但問題在於活動中沒有視圖用作活動的第一個參數(位於的的Snackbar.make())。我在那裏放什麼? 這裏的java類代碼:沒有視圖製作小吃店?

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

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

    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    mMap.setBuildingsEnabled(true); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    float cameraZoom = 17; 
    LatLng location = new LatLng(43.404032, -80.478184); 
    mMap.addMarker(new MarkerOptions().position(location).title("49 McIntyre Place #18, Kitchener, ON N2R 1G3")); 
    CameraUpdateFactory.newLatLngZoom(location, cameraZoom); 
    Snackbar.make(findViewById(/*WHAT DO I PUT HERE?*/), "Click the pin for more options", Snackbar.LENGTH_LONG).show(); 
} 
} 

而且,這裏是活動的XML代碼:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:map="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/map" 
android:name="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="ca.davesautoservice.davesautoservice.MapsActivity" /> 

最後,這裏的堆棧跟蹤錯誤:

08-03 11:42:21.333 3901-3901/? E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: ca.davesautoservice.davesautoservice, PID: 3901 
    java.lang.NullPointerException 
     at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
     at android.support.design.widget.Snackbar.make(Snackbar.java:215) 
     at ca.davesautoservice.davesautoservice.MapsActivity.onMapReady(MapsActivity.java:48) 
     at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
     at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) 
     at android.os.Binder.transact(Binder.java:361) 
     at xz.a(:com.google.android.gms.DynamiteModulesB:82) 
     at maps.ad.u$5.run(Unknown Source) 
     at android.os.Handler.handleCallback(Handler.java:808) 
     at android.os.Handler.dispatchMessage(Handler.java:103) 
     at android.os.Looper.loop(Looper.java:193) 
     at android.app.ActivityThread.main(ActivityThread.java:5333) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
     at dalvik.system.NativeStart.main(Native Method) 

感謝您的幫助! :)

+0

嘗試更換 「findViewById()」 由 – W0rmH0le

回答

31

我看到一些選項...不知道哪一個可以解決您的問題。

Simpliest

SupportMapFragment擴展android.support.v4.app.Fragment類。通過這種方式,它有一個方法getView()

Snackbar.make(mapFragment.getView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show(); 

查找根查看

From this answer,有一種方法可以通過根視圖:

getWindow().getDecorView().getRootView() 

所以,也許,你可以做:

Snackbar.make(getWindow().getDecorView().getRootView(), "Click the pin for more options", Snackbar.LENGTH_LONG).show(); 

添加一個虛擬的LinearLayout來獲取視圖

老實說,我不確定這個解決方案是否可能。我不確定您是否可以在地圖片段上方添加LinearLayout ...我認爲這樣可以,但由於之前我從未使用Maps API,因此我不確定。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/dummy_layout_for_snackbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <fragment 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="ca.davesautoservice.davesautoservice.MapsActivity" /> 
</LinearLayout> 

然後:

Snackbar.make(findViewById(R.id.dummy_layout_for_snackbar), "Click the pin for more options", Snackbar.LENGTH_LONG).show(); 
+0

無論是前兩種方法的工作 「getWindow()getDecorView()getRootView()。」精細;但爲了使用第一個,我需要將整個'Snackbar.make()'動作移動到'onCreate()'中,因爲'mapFragment'不是全局變量。發生的一件奇怪事情是,在這兩種情況下,快餐欄文本默認顯示爲黑色而不是白色,但我可以在代碼中手動更改它。 –

+10

如果使用以下方法獲取視圖'getWindow()。getDecorView()。getRootView()',則消息將顯示在底部導航欄後面。 –

1

由於流星拉烏夫指出的,獲得經由getDecorView視圖()可以把小吃吧在底部的導航條的後面。我使用下面的代碼:(使用和擴展讓你高興)

public class BaseActivity extends AppCompatActivity { 

    private View getRootView() { 
     final ViewGroup contentViewGroup = (ViewGroup) findViewById(android.R.id.content); 
     View rootView = null; 

     if(contentViewGroup != null) 
      rootView = contentViewGroup.getChildAt(0); 

     if(rootView == null) 
      rootView = getWindow().getDecorView().getRootView(); 

     return rootView; 
    } 

    protected void showSnackBarWithOK(@StringRes int res) { 
     final View rootView = getRootView(); 
     if(rootView != null) { 
      final Snackbar snackbar = Snackbar.make(getRootView(), res, Snackbar.LENGTH_INDEFINITE); 
      snackbar.setAction(R.string.ok, new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        snackbar.dismiss(); 
       } 
      }); 

      snackbar.show(); 
     } 
    } 

    protected void showSnackBar(@StringRes int res) { 
     final View rootView = getRootView(); 
     if(rootView != null) 
      Snackbar.make(rootView, res, Snackbar.LENGTH_LONG).show(); 
    } 
}