3

次のコードからユーザーの場所の名前を取得しようとしています。GUIStatics を変数に解決できないため、エラーが発生します。それを修正する方法。また、このコードが場所の名前を取得するための正しい方法であるかどうかを誰でも知ることができます。

package com.example.location;

import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.CellLocation;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {
LocationManager lmanager;
String provider;
String er="Please try again";
TextView t;
public static Context mContext; 
private double latitude,longitude;


@Override
protected void onCreate(Bundle savedInstanceState) {
    mContext=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    t=(TextView)findViewById(R.id.textView1);

    lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    lmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

    locationUpdate();


}

private void locationUpdate() {
    // TODO Auto-generated method stub
    CellLocation.requestLocationUpdate();
}

private void getAddress(double lat, double lon) {
    // TODO Auto-generated method stub
    Geocoder coder=new Geocoder(MainActivity.mContext,Locale.getDefault());
    try
    {
        List<Address> listaddress=coder.getFromLocation(lat, lon, 1);
        Address obj=listaddress.get(0);
        String add=obj.getAddressLine(0);
        GUIStatics.currentAddress=obj.getSubAdminArea()+","+obj.getAdminArea();
        GUIStatics.longitude=obj.getLongitude();
        GUIStatics.latitude=obj.getLatitude();
        GUIStatics.currentCity=obj.getSubAdminArea();
        GUIStatics.currentState=obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();
        Log.v("IGA", "Address" + add);
        Toast.makeText(this, "Address=>" + add,Toast.LENGTH_SHORT).show();


    }
    catch(Exception e)
    {
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    latitude=location.getLatitude();
    longitude=location.getLongitude();
    GUIStatics.latitude=latitude;
    GUIStatics.longitude=longitude;
    Log.v("Test", "IGA" + "Lat" + latitude + "   Lng" + longitude);
    getAddress(latitude, longitude);
    if(location!=null)
    {
        lmanager.removeUpdates(this);
    }
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}

@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
    if(status == LocationProvider.TEMPORARILY_UNAVAILABLE)
    { 
        Toast.makeText(MainActivity.this,"LocationProvider.TEMPORARILY_UNAVAILABLE", Toast.LENGTH_SHORT).show(); 
    } 
    else if(status== LocationProvider.OUT_OF_SERVICE)
    { 
     Toast.makeText(MainActivity.this, "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
    } 


}

 }

私のマニフェストファイルは

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.location"
 android:versionCode="1"
 android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.example.location.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

前もって感謝します。私を助けてください

4

0 に答える 0