Android刷新按钮不会出现在操作栏上

时间:2016-03-11 07:33:08

标签: android android-fragments

我是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) {

}

}

我一直在尝试这一天没有运气,请帮助新手解决它。提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要在setHasOptionsMenu(true); onCreate中致电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);
    }
...
}