gpt4 book ai didi

java - 如何在 Android 应用程序中运行静态内部类

转载 作者:行者123 更新时间:2023-12-01 13:55:38 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序。我在网上查了一下,并尝试了一些方法,但没有任何效果。我正在尝试在我的 Android 应用程序中运行静态内部类。它为我排序了 ArrayList。我尝试了以下方法:

GenericCachedSorter sort = new GenericCachedSorter();
sort.main(Longitude1);
sort.sort(distancetos, null);

new GenericCachedSorter();
GenericCachedSorter.main(Longitude1);

但是这两行代码都给出了 NPE。我不知道为什么这不起作用。我的代码如下:

NearestStations.java

public class Neareststations extends Activity implements LocationListener {
LocationManager mLocationManager;

double longitude;
double latitude;

String distancea;
String distance1;
static Map<String, String> myMap1 = new HashMap<String, String>();

static ArrayList<Double> distancetos = new ArrayList<Double>();

@SuppressLint("NewApi")

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.neareststations);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);

List<String> Lat1 = Arrays.asList(getResources().getStringArray(R.array.Latitude));
Log.i("Lat1", String.valueOf(Lat1.get(0)));

String[] Stations = getResources().getStringArray(R.array.Stations);
String[] Longitude = getResources().getStringArray(R.array.Longitude);

for (int h = 0; h <144; h++) {
myMap1.put(Stations[h], Longitude[h]);

}



mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
{

}
}
else {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 50, this);
Log.i("MYTAG", String.valueOf(location));
}

longitude = location.getLongitude();
latitude = location.getLatitude();
if (location != null) {

longitude = location.getLongitude();
latitude = location.getLatitude();
Log.i("MYTAG", String.valueOf(longitude));
Log.i("MYTAG", String.valueOf(latitude));


List<String> Long = Arrays.asList(getResources().getStringArray(R.array.Longitude));
List<String> Lat = Arrays.asList(getResources().getStringArray(R.array.Latitude));

Log.i("Long1", String.valueOf(Long.get(0)));

Iterator<String> iterator = Lat.iterator();
Iterator<String> iterator1 = Long.iterator();

ArrayList<Double> longitudeArray = new ArrayList<Double>();

while(iterator.hasNext()){
for (int i=0; i<144;i++){

double distance = 0;

double lat_end = 0;
double lon_end = 0;


try {
lat_end = Double.parseDouble(iterator.next());
lon_end = Double.parseDouble(iterator1.next());
longitudeArray.add((lon_end));

} catch (NumberFormatException e) {
Log.v("Main", "Convert to Double Failed : ");
}

Location locationA = new Location("point A");
locationA.setLatitude(latitude);
locationA.setLongitude(longitude);

Location locationB = new Location("point B");
locationB.setLatitude(lat_end);
locationB.setLongitude(lon_end);

distance = locationA.distanceTo(locationB) * 0.000621371192237334;

String dista = Double.toString(distance);


distancetos.add(Double.parseDouble(dista));
}
}


Collections.sort(distancetos);

distancea = distancetos.get(0).toString();
distance1 = distancetos.get(1).toString();

String Longa = longitudeArray.get(0).toString();
String Long1 = longitudeArray.get(1).toString();

String[] Stations1 = getResources().getStringArray(R.array.Stations);
String[] Longitude1 = getResources().getStringArray(R.array.Longitude);
String[] Latitude = getResources().getStringArray(R.array.Latitude);



Map<String, String> myMap = new HashMap<String, String>();{
for (int i = 0; i <144; i++) {
myMap.put(Latitude[i], Stations1[i]);
}
}

Map<String, String> myMap1 = new HashMap<String, String>();{
for (int h = 0; h <144; h++) {
myMap1.put(Longitude1[h], Stations1[h]);

}
}

String value = myMap1.get(Longa);
}
}

public void onLocationChanged(Location location) {
}


public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

public static class GenericCachedSorter {
public static void main(String[] args) {
sort(distancetos, new ToComparable<Double, Double>() {
@Override
public Double toComparable(Double distance) {
// return the longitude associated with this distance
return (Double.parseDouble(myMap1.get(distance)));
}
});

for (Double distance : distancetos)
System.out.println(distancetos);
}

public interface ToComparable<T, C extends Comparable<? super C>> {
C toComparable(T t);
}

public static <T, C extends Comparable<? super C>> void sort(List<T> list, ToComparable<T, C> function) {
class Pair implements Comparable<Pair> {
final T original;
final C comparable;

Pair(T original, C comparable) {
this.original = original;
this.comparable = comparable;
}

@Override
public int compareTo(Pair other) {
return
comparable == null && other.comparable == null ? 0 :
comparable == null ? -1 :
other.comparable == null ? 1 :
comparable.compareTo(other.comparable);
}
}

List<Pair> pairs = new ArrayList<Pair>(list.size());
for (T original : list)
pairs.add(new Pair(original, function.toComparable(original)));

Collections.sort(pairs);

ListIterator<T> iter = list.listIterator();
for (Pair pair : pairs) {
iter.next();
iter.set(pair.original);
}
}
}
}

感谢您的帮助。

最佳答案

public static void main(String[] args) 在 Android 中不可用。删除它并用另一个简单的方法调用它。

    public void SortingHere() {
sort(distancetos, new ToComparable<Double, Double>() {
@Override
public Double toComparable(Double distance) {
// return the longitude associated with this distance
return (Double.parseDouble(myMap1.get(distance)));
}
});

for (Double distance : distancetos)
System.out.println(distancetos);
}

关于java - 如何在 Android 应用程序中运行静态内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19647024/

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