gpt4 book ai didi

android - 尝试在 textViews 中显示经度和纬度

转载 作者:行者123 更新时间:2023-11-29 22:15:12 25 4
gpt4 key购买 nike

我试图在用户按下获取位置按钮后在单独的 TextView 中显示经度和纬度。所有代码对我来说似乎没问题,但我收到空指针异常。当位置仅在用户按下按钮时更改时,我不想做任何事情。

package com.seanhannon.fyp;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class FYPActivity extends Activity {

public Button getLocation;
public TextView LongCoord;
public TextView LatCoord;
public double longitude;
public double latitude;
public LocationManager lm;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener());

getLocation = (Button)getLocation.findViewById(R.id.GetLocation);
LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord);
LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord);

getLocation.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View arg0)
{

showCurrentLocation();
}



});

}


protected void showCurrentLocation()
{
// TODO Auto-generated method stub
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
latitude = location.getLatitude();
longitude = location.getLongitude();

LongCoord.setText(Double.toString(longitude));
LatCoord.setText(Double.toString(latitude));
}

}

class MyLocationListener implements LocationListener
{

@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub



}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@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

}

}

XML 文件通常具有精细和粗略位置等权限。

这是 Logcat 输出

 FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seanhannon.fyp/com.seanhannon.fyp.FYPActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.seanhannon.fyp.FYPActivity.onCreate(FYPActivity.java:33)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

最佳答案

这是您的代码和平

getLocation = (Button)getLocation.findViewById(R.id.GetLocation);
LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord);
LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord);

用下面的代码改变它

getLocation = (Button)findViewById(R.id.GetLocation);
LongCoord = (TextView)findViewById(R.id.longCoord);
LatCoord = (TextView)findViewById(R.id.LatCoord);

它将移除空指针异常...

关于android - 尝试在 textViews 中显示经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8659112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com