地理编码器初始化失败

时间:2013-03-20 10:57:38

标签: java android geolocation

尝试在我的应用程序中声明Geocoder时,我收到NullPointerException。我有以下声明:

public class MainActivity extends Activity {

private Geocoder geocoder = new Geocoder(this, Locale.getDefault());

    ...
 }

我得到以下LogCat:

03-20 10:48:55.729: D/AndroidRuntime(604): Shutting down VM
03-20 10:48:55.729: W/dalvikvm(604): threadid=1: thread exiting with uncaught exception 

(group=0x40a71930)
03-20 10:48:56.209: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-20 10:48:56.209: E/AndroidRuntime(604): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.coord/com.example.coord.MainActivity}: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.os.Looper.loop(Looper.java:137)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-20 10:48:56.209: E/AndroidRuntime(604):  at java.lang.reflect.Method.invokeNative(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604):  at java.lang.reflect.Method.invoke(Method.java:511)
03-20 10:48:56.209: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-20 10:48:56.209: E/AndroidRuntime(604):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-20 10:48:56.209: E/AndroidRuntime(604):  at dalvik.system.NativeStart.main(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604): Caused by: java.lang.NullPointerException
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
03-20 10:48:56.209: E/AndroidRuntime(604):  at com.example.coord.MainActivity.<init>(MainActivity.java:21)
03-20 10:48:56.209: E/AndroidRuntime(604):  at java.lang.Class.newInstanceImpl(Native Method)
03-20 10:48:56.209: E/AndroidRuntime(604):  at java.lang.Class.newInstance(Class.java:1319)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-20 10:48:56.209: E/AndroidRuntime(604):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

第21行是我的Geocoder声明。我的代码出了什么问题?

3 个答案:

答案 0 :(得分:3)

上下文仅在活动开始时可用,因此您无法在类主体中初始化地理编码器。尝试在onCreate或onResume方法中初始化它......

public class MainActivity extends Activity {
  private Geocoder mGeocoder;

  @Override
  protected void onCreate(Bundle _icicle) {
    super.onCreate(_icicle);
    mGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
  }
}

答案 1 :(得分:1)

将此权限添加到清单

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

并使用getApplicationContext()代替this

答案 2 :(得分:0)

建议在后台使用GeoCode,或在Google Developers页面定义的单独线程上使用。

new Thread(new Runnable() 
                    {
                        @Override
                        public void run() 
                        {
                            //Do things.
                            Geocoder geocoder = new Geocoder(getBaseContext());


                            try {
                                // Getting a maximum of 5 Address that matches the input text
                                addresses = geocoder.getFromLocationName(addressText,5);


                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }

                    }).start();