2011-10-27 131 views
0

我試圖創建一個應用程序,將採取遠程手機與GPS移動。我的問題是,當我點擊按鈕b1時,它不會停止監聽,並且不會再進行任何點擊。Android按鈕onClick只能工作一次

我在做什麼錯? package 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 

    } 

}; 

}

回答

3

這是因爲你改變的內容視圖,這意味着對以前的佈局任何按鈕現在是無效的。如果您想啓動活動,則應該使用startActivity()startActivityForResult()而不是更改內容視圖。在所有(大多數?可能有例外情況)情況下,每次活動只應調用一次setContentView()