从谷歌获取纬度和经度的位置详细信息

时间:2014-04-15 03:40:30

标签: android google-maps

我在做什么 ::

我试图通过传递我现在的纬度和经度来记录当前位置


MainActivity.java

public class MainActivity extends Activity {

    TextView txt;
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=(TextView) findViewById(R.id.txtID);
        Log.d("-----Before-Calling-getAddress-----", result);
        result=getAddress(12.918286,77.669493);
        Log.d("-----After-Calling-getAddress-----", result);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        txt.setText(result);
    }


    private String getAddress(double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                result.append(address.getLocality()).append("\n");
                result.append(address.getCountryName());
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }
}

日志 ::

04-15 09:03:22.862: E/AndroidRuntime(965): FATAL EXCEPTION: main
04-15 09:03:22.862: E/AndroidRuntime(965): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.getlocationdetails/com.example.getlocationdetails.MainActivity}: java.lang.NullPointerException: println needs a message
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.os.Looper.loop(Looper.java:137)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-15 09:03:22.862: E/AndroidRuntime(965):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:03:22.862: E/AndroidRuntime(965):  at java.lang.reflect.Method.invoke(Method.java:511)
04-15 09:03:22.862: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-15 09:03:22.862: E/AndroidRuntime(965):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-15 09:03:22.862: E/AndroidRuntime(965):  at dalvik.system.NativeStart.main(Native Method)
04-15 09:03:22.862: E/AndroidRuntime(965): Caused by: java.lang.NullPointerException: println needs a message
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.util.Log.println_native(Native Method)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.util.Log.d(Log.java:138)
04-15 09:03:22.862: E/AndroidRuntime(965):  at com.example.getlocationdetails.MainActivity.onCreate(MainActivity.java:25)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.Activity.performCreate(Activity.java:4465)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-15 09:03:22.862: E/AndroidRuntime(965):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-15 09:03:22.862: E/AndroidRuntime(965):  ... 11 more

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.getlocationdetails"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.getlocationdetails.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="****************************************" />

    </application>

</manifest>

{编辑后} - 添加String result=" ";

04-15 09:19:19.522: D/-----Before-Calling-getAddress-----(1222):  
04-15 09:19:19.522: D/-----Before-Calling-getAddress-----(1222): +++ LOG: entry corrupt or truncated
04-15 09:19:19.672: W/dalvikvm(1234): No implementation found for native Lcom/android/inputmethod/pinyin/PinyinDecoderService;.nativeImOpenDecoderFd (Ljava/io/FileDescriptor;JJ[B)Z
04-15 09:19:19.702: D/AndroidRuntime(1234): Shutting down VM
04-15 09:19:19.702: W/dalvikvm(1234): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-15 09:19:19.752: I/WindowManager(91): createSurface Window{415e6518 com.example.getlocationdetails/com.example.getlocationdetails.MainActivity paused=false}: DRAW NOW PENDING
04-15 09:19:19.832: E/AndroidRuntime(1234): FATAL EXCEPTION: main
04-15 09:19:19.832: E/AndroidRuntime(1234): java.lang.UnsatisfiedLinkError: nativeImOpenDecoderFd
04-15 09:19:19.832: E/AndroidRuntime(1234):     at com.android.inputmethod.pinyin.PinyinDecoderService.nativeImOpenDecoderFd(Native Method)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at com.android.inputmethod.pinyin.PinyinDecoderService.initPinyinEngine(PinyinDecoderService.java:141)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at com.android.inputmethod.pinyin.PinyinDecoderService.onCreate(PinyinDecoderService.java:162)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2252)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.app.ActivityThread.access$1600(ActivityThread.java:122)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.os.Looper.loop(Looper.java:137)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at android.app.ActivityThread.main(ActivityThread.java:4340)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-15 09:19:19.832: E/AndroidRuntime(1234):     at dalvik.system.NativeStart.main(Native Method)
04-15 09:19:20.092: D/gralloc_goldfish(1222): Emulator without GPU emulation detected.
04-15 09:19:20.222: I/WindowManager(91): createSurface Window{416241d8  paused=false}: DRAW NOW PENDING
04-15 09:19:20.812: W/TextLayoutCache(1222): computeValuesWithHarfbuzz -- need to force to single run
04-15 09:19:20.972: I/ActivityManager(91): Displayed com.example.getlocationdetails/.MainActivity: +4s40ms
04-15 09:19:21.522: W/NetworkManagementSocketTagger(91): setKernelCountSet(10005, 0) failed with errno -2
04-15 09:19:22.772: I/Process(1234): Sending signal. PID: 1234 SIG: 9
04-15 09:19:22.863: I/ActivityManager(91): Process com.android.inputmethod.pinyin (pid 1234) has died.
04-15 09:19:22.863: W/ActivityManager(91): Service crashed 2 times, stopping: ServiceRecord{41670250 com.android.inputmethod.pinyin/.PinyinIME}
04-15 09:19:22.932: W/InputManagerService(91): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4150b428

我如何解决此问题 !!

1 个答案:

答案 0 :(得分:2)

您必须在活动中初始化String result=" ";。它是becoz

Log.d("-----Before-Calling-getAddress-----", result);// at this position result=null

result=getAddress(12.918286,77.669493);

Log.d("-----After-Calling-getAddress-----", result);// at this position get result