Android:GPS应用程序的空指针异常

时间:2013-03-18 18:59:03

标签: android gps nullpointerexception location

我尝试为我的学习创建一个跟踪运动的应用程序。

当我发送新的本地化时,我的应用程序因此错误而崩溃:

03-18 18:44:34.662: E/AndroidRuntime(1085): FATAL EXCEPTION: main
03-18 18:44:34.662: E/AndroidRuntime(1085): java.lang.NullPointerException
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.app.Activity.findViewById(Activity.java:1839)
03-18 18:44:34.662: E/AndroidRuntime(1085): at fr.polytech.trackersportif.GPSData.onLocationChanged(GPSData.java:28)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.os.Looper.loop(Looper.java:137)
03-18 18:44:34.662: E/AndroidRuntime(1085): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-18 18:44:34.662: E/AndroidRuntime(1085): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:44:34.662: E/AndroidRuntime(1085): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 18:44:34.662: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 18:44:34.662: E/AndroidRuntime(1085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 18:44:34.662: E/AndroidRuntime(1085): at dalvik.system.NativeStart.main(Native Method)

我的主要活动是:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new GPSData();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, locationListener);

完成我的班级(GPSData)是:

public class GPSData extends Activity implements LocationListener{

 TextView textLong, textLat;



protected void onCreate(Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);     
    setContentView(R.layout.page_accueil);  //page_acceuil is the layout where are the two textview that I want set text  
}

@Override
public void onLocationChanged(Location location) {

     textLong = (TextView) findViewById(R.id.textView1);
     textLat = (TextView) findViewById(R.id.textView3);

    textLong.setText(String.valueOf(location.getLongitude()));
    textLat.setText(String.valueOf(location.getLatitude()));        
}

我从两天开始尝试不同的事情,但我一次又一次地犯这个错误。

你能帮我吗?

3 个答案:

答案 0 :(得分:0)

您无法在模拟器中测试基于地理位置的应用,而无需使用telnetgeo fix lat coordinate long coordinate命令为其指定lat长坐标。

答案 1 :(得分:0)

您必须将GPSData放入主要活动中。你只能访问(TextView)findViewById(R.id.textView1);在MainActivity中

和MainActivity.this.findViewById(R.id.textView1);

答案 2 :(得分:0)

调用new不是启动Activity的正确方法。您NullPointerException最有可能被抛弃:

textLong.setText(String.valueOf(location.getLongitude()));

这是因为通过调用Activity创建新的new不会触发onCreate方法。因此,您的GPSData类永远不会设置视图,因为未调用setContentView。这意味着使用您自己的一个视图ID调用findViewById将始终返回null

我建议您在LocationListener课程中进行操作,而不是从MainActivity设置GPSData。为此,您必须进行以下更改:

MainActivity:

Intent intent = new Intent(this, GPSData.class);
startActivity(intent);

GPSData:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.page_accueil);

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
}

您应该能够独自离开GPSData课程的其余部分。只更改onCreate方法以匹配此方法。

这就是假设您确实希望GPSData成为单独的Activity,就像设置代码一样。如果您不希望它是单独的Activity,那么我建议将GPSData作为私有内部类,并使其不像BrianPlummer建议的那样扩展Activity。在这种情况下,您的GPSData课程将如下所示:

private class GPSData implements LocationListener {

    TextView textLong;
    TextView textLat;

    public GPSData() {
        textLong = (TextView) findViewById(R.id.textView1);
        textLat = (TextView) findViewById(R.id.textView3);
    }

    @Override
    public void onLocationChanged(Location location) {    
        textLong.setText(String.valueOf(location.getLongitude()));
        textLat.setText(String.valueOf(location.getLatitude()));        
    }
}