编码只能在Button的onClick事件中工作

时间:2012-02-21 13:40:08

标签: android

我的以下编码获取当前地址意味着如果我尝试将地址设置为TextBox但是当我将该代码放入Button的onClick事件中时,位置失败

这是编码工作当它在onCLick事件中时

import java.io.IOException;
import java.util.*;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.location.*;
import android.content.*;

public class location extends Activity {  

 Button addressButton;
 TextView addressText;
 Location currentLocation;
 double currentLatitude;
 double currentLongitude;
 @Override

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.location);  
     addressText = (TextView)findViewById(R.id.addressText);
     addressButton = (Button)findViewById(R.id.addressButton);
     this.addressText.setText("ready");

    LocationManager locationManager = 
        (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }
        public void onStatusChanged(
                String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
    };
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    this.addressButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            try{
                Geocoder gcd = new Geocoder(location.this, Locale.getDefault());
                List<Address> addresses = 
                    gcd.getFromLocation(currentLatitude, currentLongitude,100);
                StringBuilder result = new StringBuilder();
                if (addresses.size() > 0) {      
                        Address address =  addresses.get(0);
                        int maxIndex = address.getMaxAddressLineIndex();
                        for (int x = 0; x <= maxIndex; x++ ){
                            result.append(address.getAddressLine(x));
                            //result.append(",");
                        }                    
                    }
                    addressText.setText(result.toString()); 
                    Intent send_add = new Intent();
                    send_add.putExtra("address",result.toString());
            }
            catch(IOException ex){
                addressText.setText(ex.getMessage().toString());
            }
        }
    });
} 
void updateLocation(Location location){
    currentLocation = location;
    currentLatitude = currentLocation.getLatitude();
    currentLongitude = currentLocation.getLongitude();
}}

这是Coding not working here我不想在我打开这个活动时使用Button它必须显示直接地址意味着它应该设置为Textview

public class location extends Activity {  
Button addressButton;
TextView addressText;
Location currentLocation;
double currentLatitude;
double currentLongitude;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);  
    addressText = (TextView)findViewById(R.id.addressText);
    addressButton = (Button)findViewById(R.id.addressButton);
    this.addressText.setText("ready");

    LocationManager locationManager = 
        (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }
        public void onStatusChanged(
                String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
    };
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

            try{
                Geocoder gcd = new Geocoder(location.this, Locale.getDefault());
                List<Address> addresses = 
                    gcd.getFromLocation(currentLatitude, currentLongitude,100);
                StringBuilder result = new StringBuilder();
                if (addresses.size() > 0) {      
                        Address address =  addresses.get(0);
                        int maxIndex = address.getMaxAddressLineIndex();
                        for (int x = 0; x <= maxIndex; x++ ){
                            result.append(address.getAddressLine(x));
                            //result.append(",");
                        }                    
                    }
                    addressText.setText(result.toString()); 
                    Intent send_add = new Intent();
                    send_add.putExtra("address",result.toString());
            }
            catch(IOException ex){
                addressText.setText(ex.getMessage().toString());
            }
} 
void updateLocation(Location location){
    currentLocation = location;
    currentLatitude = currentLocation.getLatitude();
    currentLongitude = currentLocation.getLongitude();
}}

在这里,我做了一个小改动,但没有工作

1 个答案:

答案 0 :(得分:1)

我已更新您的代码,现在工作正常:

        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); //added by me
      //  Geocoder gcd = new Geocoder(location.this, Locale.getDefault());
        try{
           // Geocoder gcd = new Geocoder(location.this, Locale.getDefault());
            //List<Address> addresses = 
               // gcd.getFromLocation(currentLatitude, currentLongitude,100);
            List<Address> addresses = new Geocoder(location.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);          // changed
            StringBuilder result = new StringBuilder();
            if (addresses.size() > 0) {      
                    Address address =  addresses.get(0);
                    int maxIndex = address.getMaxAddressLineIndex();
                    for (int x = 0; x <= maxIndex; x++ ){
                        result.append(address.getAddressLine(x));
                        //result.append(",");
                    }                    
                }
                addressText.setText(result.toString()); 
                Intent send_add = new Intent();
                send_add.putExtra("address",result.toString());
        }
        catch(IOException ex){


   addressText.setText(ex.getMessage().toString());
    }