2016-03-11 47 views
1

我是Android新手,正在處理碎片和操作欄和菜單。我在菜單中放置了一個刷新按鈕,我從我的片段中放大了它,但沒有顯示。這是我的代碼。有人可以告訴我我做錯了什麼嗎?Android刷新按鈕不會出現在操作欄上

菜單

<?xml version="1.0" encoding="utf-8"?> 

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:id="@+id/action_reload" 
      android:title="@string/action_reload" 
      android:showAsAction="always" android:icon="@drawable/nav_btn_reload"/> 
</menu> 

片段

public class NearbyFragment extends BaseFragment implements OnConnectionFailedListener, LocationListener, LoaderCallbacks<List<User>> { 

    LocationClient locationClient; 
    Location lastLocation; 
    private GoogleMap mMap; 
    private HashMap<Marker, User> mHashMap = new HashMap<Marker, User>(); 
    private static View view; 
    public void onResume() { 

      super.onResume(); 
      getActivity().invalidateOptionsMenu(); 


      //setHasOptionsMenu(false); 
     } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getActivity().getActionBar().setCustomView(R.layout.actionbar_main); 


     if (view != null) { 
      ViewGroup parent = (ViewGroup) view.getParent(); 
      if (parent != null) 
       parent.removeView(view); 
     } 
     try { 
      view = inflater.inflate(com.company.stush.R.layout.fragment_nearby, container, false); 
     } catch (InflateException e) { 
      /* map is already there, just return view as it is */ 
     } 
     return view; 
    } 






    @Override 
    public void onActivityCreated (Bundle savedInstanceState) { 

     super.onActivityCreated(savedInstanceState); 

     //setHasOptionsMenu(true); 

     LocationManager manager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
     boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     if(!statusOfGPS) 
     { 
      Toast.makeText(getActivity(), "Turn on 'Location Services' to acces this feature.", 
         Toast.LENGTH_LONG).show(); 
     } 


     mMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(com.company.stush.R.id.map)).getMap(); 


     mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 

@Override 
public void onInfoWindowClick(Marker marker) { 

    User user = mHashMap.get(marker); 
    Intent intentUser = new Intent(getActivity(), ProfileActivity.class); 
    intentUser.putExtra("userID",user.userID); 
    getActivity().startActivity(intentUser); 
    //Log.d("GODD", "Click"); 
      } }); 




     locationClient = new LocationClient(getActivity(), new GooglePlayServicesClient.ConnectionCallbacks() { 




      @Override 


      public void onDisconnected() { 
       Log.d("Eror1", "Disconect"); 

      } 

      @Override 
      public void onConnected(Bundle connectionHint) { 
       LocationRequest locationRequest = LocationRequest.create(); 
       locationClient.requestLocationUpdates(locationRequest, NearbyFragment.this); 
      } 
     }, this); 
     locationClient.connect(); 
     mMap.setMyLocationEnabled(true); 
    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 

     inflater.inflate(com.company.stush.R.menu.nearby, menu); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.d("Eror1", "Disabled GPS"); 

    } 

    @Override 
    public void onLocationChanged(Location location) { 
     Log.d("Eror", "Disabled GPS"); 
     if (lastLocation == null || location.distanceTo(lastLocation) > 1000) { 
      lastLocation = location; 
      getLoaderManager().initLoader(0, null, this); 

     } 
    } 



public void onProviderDisabled(String provider) { 
    Log.d("Eror", "Disabled GPS"); 

} 





@Override 
public Loader<List<User>> onCreateLoader(int arg0, Bundle arg1) { 
    NearbyLoader loader = new NearbyLoader(getActivity(), this.lastLocation); 
    return loader; 
} 

@Override 
public void onLoadFinished(Loader<List<User>> arg0, List<User> users) { 
    Log.d("USERS", users.toString()); 
    for (User user : users) { 
     MarkerOptions options = new MarkerOptions(); 
     //.title("You are here")   
     options.icon(BitmapDescriptorFactory.fromResource(com.company.stush.R.drawable.map_pin)); 
     options.position(new LatLng(user.latitude, user.longitude)); 
     options.title(user.userFullName); 
     options.snippet(user.companyAddress); 

     Marker m =mMap.addMarker(options); 

     mHashMap.put(m, user); 
    } 
} 

@Override 
public void onLoaderReset(Loader<List<User>> arg0) { 

} 

}

我一直在試圖爲這個沒有運氣一天,請大家幫一個新手解決。先謝謝你。

回答

2

你需要撥打onCreatesetHasOptionsMenu(true);Fragment顯示ActionBar

public class NearbyFragment extends BaseFragment implements OnConnectionFailedListener, LocationListener, LoaderCallbacks<List<User>> { 
... 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     // TODO Add your menu entries here 
     inflater.inflate(com.company.stush.R.menu.nearby, menu); 
     super.onCreateOptionsMenu(menu, inflater); 
    } 
... 
} 
+1

真棒真棒bro..perfect .. :) –