每次我点击按钮rangeincrement和rangeDecrement时,应用程序都会停止。谁有解决方案?

时间:2015-11-08 04:45:18

标签: android android-fragments import onclicklistener

我正在尝试在片段中单击按钮时编写代码,但按钮不起作用。按钮设置为增加和减少textview(rangeText)的值。

我已将OnClickListener实现到我的片段。

我想知道错误在哪里。

    package com.neoresearch.bookmyspace;


    import android.content.Context;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AutoCompleteTextView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.PopupWindow;
    import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * A fragment that launches other parts of the demo application.
 */
public class ParkingFinderMapFragment extends Fragment implements View.OnClickListener {

    LinearLayout layoutOfPopup;
    PopupWindow popupMessage;
    Button popupButton, insidePopupButton, searchButton, rangeIncrement, rangeDecrement;
    TextView rangeText;
    TextView name, contact, address, availability;
    AutoCompleteTextView searchText;
    LatLng latLng;
    MarkerOptions markerOptions;
    MapView mMapView;
    GoogleMap googleMap;
    private EditText editText;
    String string;
    private View v;
     int range;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        v = inflater.inflate(R.layout.fragment_parking_finder_map, container,
                false);
        searchText = (AutoCompleteTextView) v.findViewById(R.id.search_on_map);
        searchButton = (Button) v.findViewById(R.id.button_search);
        rangeIncrement = (Button) v.findViewById(R.id.range_increment);
        rangeDecrement = (Button) v.findViewById(R.id.range_decrament);
        rangeText = (TextView) v.findViewById(R.id.range_select);




        mMapView = (MapView) v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mMapView.getMap();
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;

        // create marker
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
        LatLng indore = new LatLng(22.7000, 75.9000);

        final MarkerOptions marker2 = new MarkerOptions().position(indore).title("Indore");
        googleMap.addMarker(marker2);

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(22.7000, 75.9000)).zoom(8).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

        // Perform any camera updates here

        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                PopupOnMap pop = new PopupOnMap(getActivity());
                pop.show();
                pop.setName("prafull", marker.getTitle(), "8269564260");
                return false;
            }
        });

        searchButton.setOnClickListener(this);   
        rangeDecrement.setOnClickListener(this);//these are my buttons
        rangeIncrement.setOnClickListener(this);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

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

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }


//here is onclick listeners

    @Override
    public void onClick(View v) {


        switch (v.getId()) {


            case R.id.button_search:


                break;

            case R.id.range_increment:

                range = Integer.parseInt(rangeText.getText().toString());
                if (range > 0 && range < 20) {
                    range = range++;
                }
                rangeText.setText(range);
                break;

            case R.id.range_decrament:

                range = Integer.parseInt(rangeText.getText().toString());
                if (range > 0 && range < 20) {
                    range = range--;
                }
                rangeText.setText(range);
                break;


        }

    }
}

1 个答案:

答案 0 :(得分:0)

onClick()方法进行以下更改:

         case R.id.range_increment:

            range = Integer.parseInt(rangeText.getText().toString());
            if (range > 0 && range < 20) {
                range = range++;
            }
            rangeText.setText(String.valueOf(range));
            break;

        case R.id.range_decrament:

            range = Integer.parseInt(rangeText.getText().toString());
            if (range > 0 && range < 20) {
                range = range--;
            }
            rangeText.setText(String.valueOf(range));
            break;