0

我有一个包含地点及其坐标的数据库。我想创建片段,我将在其中列出对我来说超过 5 公里的地方列表。我的代码是:

我从下一个开始片段:

     case R.id.btn_near:
        if(isOnline()){
        ft = getSupportFragmentManager().beginTransaction().replace(
            R.id.content_frame, frg);
        ft.commit();
        }
        else 
            Toast.makeText(this, "Подключите интернет", 1000).show();
        break;

FragmentNear.class

public class FragmentNear extends Fragment implements
    android.location.LocationListener {
Cursor c;
View v;
ListView  lvMore5;
Database db;
protected LocationManager locationManager;
String lat;
String provider;
protected String latitude, longitude;
protected boolean gps_enabled, network_enabled;
double myLat, myLong;
SimpleCursorAdapter scAdapter;
String[] from;
int[] to;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Log.d("myLogs", "-------- onCreateView " + myLat + " " + myLong);
    v = inflater.inflate(R.layout.fragment_near, null);
    lvMore5 = (ListView) v.findViewById(R.id.lv_more_5);
    Log.d("myLogs", "-------- onCreateViewEnd " + myLat + " " + myLong);
    return v;
}

@Override
public void onResume() {
    db = new Database(getActivity());
    db.open();
    from = new String[] { Database.PLACE_NAME, Database.PLACE_DISTANCE,
            Database.PLACE_ADRESS };
    to = new int[] { R.id.tv_place_name_n, R.id.tv_distance,
            R.id.tv_place_adress_n };
    Log.d("myLogs", "-------- onCreate " + myLat + " " + myLong);
    locationManager = (LocationManager) getActivity().getSystemService(
            Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, this);
    super.onResume();
}

public double getDistance(double myLat, double myLong, double pointLat,
        double pointLong) {
    Log.d("myLogs", "getDistance =" + myLat + " " + myLong);
    double distance = 0;
    Location locationA = new Location("A");
    // 69.019207,33.085015",
    locationA.setLatitude(myLat);
    locationA.setLongitude(myLong);
    Location locationB = new Location("B");
    locationB.setLatitude(pointLat);
    locationB.setLongitude(pointLong);
    distance = locationA.distanceTo(locationB);
    return distance / 1000;

}

@Override
public void onLocationChanged(Location location) {
    final Location loc = location;
    myLat = loc.getLatitude();
    myLong = location.getLongitude();
    Log.d("myLogs", "-------- onLoChangedEnd " + myLat + " " + myLong);
    calculateDistance();---I want to calculate distance after i get my location

}

@Override
public void onStop() {
    db.close();
    if (c != null) {
        c.close();
    }
    super.onStop();
}

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

}

public void calculateDistance() {
    c = db.getPlaceByGroup(3);
    if (c != null) {
        if (c.moveToFirst()) {

            do {

                String s = c.getString(8);
                double lat, lon;
                lat = Double.valueOf(s.substring(0, s.indexOf(",")));
                lon = Double.valueOf(s.substring(s.indexOf(",") + 1,
                        s.length()));

                Log.d("myLogs",
                        "id= " + c.getInt(0) + "Coordinates"
                                + c.getString(8));// !!

                ContentValues cv = new ContentValues();
                cv.put(Database.PLACE_DISTANCE,
                        getDistance(myLat, myLong, lat, lon));
                int updCount = db.mDB.update(Database.DB_TABLE_PLACES, cv,
                        "_id = " + db.getPlaceId(3, c.getPosition()), null);
                Log.d("myLogs", "updated rows count = " + updCount);

            } while (c.moveToNext());
            // Log.d("myLogs","N1ame =" +c.getString(1));
        }
    } else
        Log.d("myLogs", "Cursor is null");


    c = db.getPlacesMore5000();
    scAdapter = new SimpleCursorAdapter(getActivity(),
            R.layout.item_near_list, c, from, to);
    lvMore5.setCacheColorHint(Color.TRANSPARENT);
    lvMore5.setAdapter(scAdapter);
    lvMore5.setOnTouchListener(new ListView.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }

            // Handle ListView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    lvMore5.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Fragment fragment = new FragmentPlaceInfo();
            db.open();
            Bundle data = new Bundle();
            c = db.getPlacesMore5000();
            if (c != null) {
                c.moveToPosition(position);
                data.putInt("placeId", c.getInt(0));
            } else
                Log.d("myLogs", "Cursor is null");

            fragment.setArguments(data);
            FragmentTransaction transaction = getFragmentManager()
                    .beginTransaction();

            transaction.replace(R.id.content_frame, fragment);
            transaction.addToBackStack(null);
            transaction.commit();

            // /less 1

        }

    });
}

@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

}

 }

真实设备 - wgen 我按下 R.id.btn_near - 一切都很好,当我再次按下它时(我想再次运行片段) - 我得到错误

    08-23 17:52:39.928: E/AndroidRuntime(2953): FATAL EXCEPTION: main
08-23 17:52:39.928: E/AndroidRuntime(2953): java.lang.NullPointerException
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:56)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:63)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at com.example.cars51ru.FragmentNear.showMFD(FragmentNear.java:352)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at com.example.cars51ru.FragmentNear.onLocationChanged(FragmentNear.java:124)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.os.Looper.loop(Looper.java:123)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at android.app.ActivityThread.main(ActivityThread.java:3839)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at java.lang.reflect.Method.invoke(Method.java:507)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-23 17:52:39.928: E/AndroidRuntime(2953):     at dalvik.system.NativeStart.main(Native Method)

在这一行:scAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item_near_list, c, from, to);

当我在模拟器上启动它时 - 我得到空的 listView。

4

1 回答 1

0

尝试将 from 和 to 放在适配器的 init 之前,如下所示:

db = new Database(getActivity());
db.open();
from = new String[] { Database.PLACE_NAME, Database.PLACE_DISTANCE,
        Database.PLACE_ADRESS };
to = new int[] { R.id.tv_place_name_n, R.id.tv_distance,
        R.id.tv_place_adress_n };
c = db.getPlacesMore5000();
scAdapter = new SimpleCursorAdapter(getActivity(),
        R.layout.item_near_list, c, from, to);
于 2013-08-23T15:48:55.077 回答