0

我正在尝试制作一个获取位置并发送该数据的应用程序,直到 10 分钟前一切都很好。现在我得到一个错误,我找不到它是什么。任何帮助,谢谢...

下面是我的一些代码...

...
...
     Location mCurrentLocation;

private final static int
        CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
LocationClient mLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
    /*
     * Create a new location client, using the enclosing class to
     * handle callbacks.
     */
    mLocationClient = new LocationClient(this, this, this);
}



...
...


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        TextView tv = (TextView) findViewById(R.id.textView1);
         mCurrentLocation = mLocationClient.getLastLocation();
        tv.setText("slm"+mCurrentLocation.getLatitude());
    }

我得到的错误是......

09-19 10:58:10.320: E/AndroidRuntime(1542): FATAL EXCEPTION: main
09-19 10:58:10.320: E/AndroidRuntime(1542): java.lang.NullPointerException
09-19 10:58:10.320: E/AndroidRuntime(1542):     at com.example.testapplication.MainActivity.onClick(MainActivity.java:186)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.view.View.performClick(View.java:2538)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.view.View$PerformClick.run(View.java:9152)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.os.Handler.handleCallback(Handler.java:587)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.os.Looper.loop(Looper.java:130)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at android.app.ActivityThread.main(ActivityThread.java:3693)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at java.lang.reflect.Method.invoke(Method.java:507)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-19 10:58:10.320: E/AndroidRuntime(1542):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

在 TextView 中设置位置纬度之前检查 null。您不能确定它会getLastLocation()立即返回一个有效的位置对象,因为它是一个非阻塞调用。

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    TextView tv = (TextView) findViewById(R.id.textView1);
     mCurrentLocation = mLocationClient.getLastLocation();
    if(mCurrentLocation != null)
        tv.setText("slm"+mCurrentLocation.getLatitude());
}
于 2013-09-19T08:14:16.173 回答
1

谢谢你们帮助激活谷歌的位置服务解决了我的问题。

于 2013-09-19T08:23:09.370 回答