-1

こんにちは、私はここで問題を抱えています

視点データベースの追加および編集フォームがあり、経度と緯度の座標用に2つのEditTextがあり、ボタンをクリックすると現在の座標を取得するボタンがあり、2つのEditTextに座標が入力され、ユーザーはデータの挿入または編集に進むことができます

私のコードは java.nullPointerException を返します。理由はわかりません...

これは私のコードです:

btn_getkoor.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try{

                    LocationListener gpsLocation = new MyLocationListener();
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsLocation); //new MyLocationListener());
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    final TextView error_text = (TextView) findViewById(R.id.error_text);

                    if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                        locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
                }catch(Exception e){
                    toastkeun(e.toString());
                }

            }
        });

解決策はありますか?

私はすでにマニフェストファイルを設定しています 参考までに

4

2 に答える 2

0

ただし、コードから言うのは難しいです:

これを行っていない場合は、コードを次に示します。

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     
于 2011-07-04T04:24:15.047 に答える
0
     LocationManager locationManager;
    String bestProvider;
    String LOCATION_SERVICE="location" ; 
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        //criteria.setAccuracy();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);  // More accurate, GPS fix.
        bestProvider = locationManager.getBestProvider(criteria,true);
    location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                       // locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
于 2011-07-04T04:34:27.097 に答える