简体   繁体   English

Android按钮onClick仅可使用一次

[英]Android button onClick works only once

Im trying to create an app that will take the distance the phone moves with gps. 我正在尝试创建一个应用程序,该应用程序将使手机以gps移动。 My problem is that when I click the button b1 it wont this listener stops listening and wont take any further clicks. 我的问题是,当我单击按钮b1时,此监听器将不会停止监听,也不会再进行任何单击。

What am I doing wrong? 我究竟做错了什么? package com.HowMuchHowFar; 包com.HowMuchHowFar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.location.*;

public class HowMuchHowFarActivity extends Activity {
//declare variables
double lastlon,lastlat,curlon,curlat=0.0;
float total_distance=0; 
float dist[]= new float[1];
boolean k2m_check=true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create button
    Button b1=(Button) findViewById(R.id.firstbtn);

    System.out.println("hhhh");

    //Open start activity
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //change to next activity
            setContentView(R.layout.start);

            //Create kilometer to meter button
            Button k2m=(Button) findViewById(R.id.Kilometer);

            //get access to text field
            TextView display = (TextView) findViewById(R.id.display);

            //start location manager
            LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
            lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
            Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

            //Prepare for null value and force close
            if(loc==null){
                display.setText("No GPS location found");
                }
                else{
                    //set Current latitude and longitude
                    curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
                    curlat=Double.valueOf((String.valueOf(loc.getLatitude())));

                    }
            //Set the last latitude and longitude
            lastlon=curlon;
            lastlat=curlat;

            //Start kilometer to meter button listener

            k2m.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //start text check
                      //create button 
                    Button k2m1=(Button) findViewById(R.id.Kilometer);

                    //get access to text field
                    TextView disp1 = (TextView) findViewById(R.id.display);

                    if(k2m_check==true){
                        //Change text
                        k2m1.setText("Meters");
                        onDestroy();

                        //startActivity();
                        //Set boolean for the next change
                        k2m_check=false;

                        disp1.setText(total_distance/1000+" Kilometers");

                    }

                    if(k2m_check==false){
                        //Change text
                        k2m1.setText("Kilometers");

                        //Set boolean for next change
                        k2m_check=true;

                        //Set display
                        disp1.setText(total_distance*1000+" Meters");
                    }//end text check

                }
            });//end k2m setonclicklistener


        }
    });


}//ends on create
LocationListener Loclist=new LocationListener(){

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        //start location manager
        LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
        TextView display1 = (TextView) findViewById(R.id.display);

        //Create display for kilometer cost
        TextView display_cpk = (TextView) findViewById(R.id.display_cpk);
        TextView display_totalcost = (TextView) findViewById(R.id.display_totalcost);

        //Get last location
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);            

        //Request new location
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);

        //Get new location
        Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        //get the current lat and long
        curlon=Double.valueOf((String.valueOf(loc.getLongitude())));
        curlat=Double.valueOf((String.valueOf(loc.getLatitude())));

        //Get distance between 2 locations put the distance into dist[0], in meters
        Location.distanceBetween(lastlat,lastlon,curlat,curlon,dist);

        //add the distance from this update to the total distance
        total_distance=dist[0]+total_distance;

        //Show distance travelled           
        display1.setText("Total distance "+total_distance+" Meters");

        //Get cost per kilometer
        CharSequence h=display_cpk.getText();
        double mul=Double.parseDouble(h.toString());

        //perform multipication and output cost
        double cost=mul*(total_distance/1000);
        display_totalcost.setText(String.valueOf(cost));
        System.out.println(dist[0]);
        System.out.println(mul);
        System.out.println(total_distance/1000);


    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status,
            Bundle extras) {
        // TODO Auto-generated method stub

    }

};

} }

It's because you're changing the content view, meaning any buttons on the previous layout are now invalid. 这是因为您正在更改内容视图,这意味着以前布局上的所有按钮现在都无效。 If you're wanting to launch an activity, you should do so with startActivity() or startActivityForResult() rather than changing the content view. 如果要启动活动,则应使用startActivity()startActivityForResult()来启动,而不要更改内容视图。 In all(Most? There may be exceptions) cases, you should only be calling setContentView() one time per activity. 在所有情况下(大多数?可能会有例外),每个活动只应调用一次setContentView()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM