0

私は、追跡スポーツである私の研究用のアプリケーションを作成しようとしています。

新しいローカリゼーションを送信すると、次のエラーが原因でアプリケーションがクラッシュします:

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)

私のmainActivityは:

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()));        
}

2日以来、さまざまなことを試していますが、このエラーが何度も発生しています。

助けてください。

4

3 に答える 3

0

呼び出しnewは、 を開始する適切な方法ではありませんActivity。あなたNullPointerExceptionはおそらくラインに投げ込まれています:

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

これは、Activity呼び出して新しいを作成しnewてもメソッドがトリガーされないためonCreateです。したがって、GPSDataクラスはsetContentView呼び出されていないため、ビュー セットはありません。つまりfindViewById、独自のビュー ID のいずれかで呼び出すと、常に が返されnullます。

LocationListenerからセットアップする代わりに、クラスでセットアップするMainActivityことをお勧めしますGPSData。そのためには、次の変更を行う必要があります。

主な活動:

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

GPS データ:

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別のものにしたくない場合は、プライベートな内部クラスを作成し、BrianPlummer が提案したように拡張しないことActivityをお勧めします。その場合、クラスは次のようになります。GPSDataActivityGPSData

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()));        
    }
}
于 2013-03-18T19:09:37.630 に答える
0

コマンドを使用telnetして緯度経度座標を指定せずに、エミュレーターで地理位置情報ベースのアプリをテストすることはできません。geo fix lat coordinate long coordinate

于 2013-03-18T19:03:23.210 に答える
0

GPSData をメインアクティビティに入れる必要があります。(TextView) findViewById(R.id.textView1); にのみアクセスできます。MainActivity で

そして MainActivity.this.findViewById(R.id.textView1);

于 2013-03-18T19:04:27.287 に答える