0

尝试在我的应用程序中声明地理编码器时出现 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 声明。我的代码有什么问题?

4

3 回答 3

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());
  }
}
于 2013-03-20T11:48:35.667 回答
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() insted ofthis

于 2013-03-20T11:04:00.803 回答
0

建议在后台或谷歌开发者页面定义的单独线程上使用 GeoCode。

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();
于 2014-09-30T09:27:34.430 回答