gpt4 book ai didi

java - 安卓java : accessing class that has parentActivity

转载 作者:行者123 更新时间:2023-12-01 11:40:17 25 4
gpt4 key购买 nike

我有 GPS 监听器类 AppLocationListener,它具有父 Activity GPSSensorActivity(下面未显示)。

我正在尝试使用 AppLocationListener searchLL = new AppLocationListener(); 从另一个类 QuestionSearch 访问 AppLocationListener searchLL.parentActivity = this; 但使用 this 会导致 QuestionSearchGPSSensorActivity 之间的类型不匹配。 .

这是因为无法访问具有父 Activity 的类吗?如何访问AppLocationListenerQuestionSearch 是我添加到现有代码中的东西,这不是我的原始代码,我正在尝试我是 java 新手,还不完全理解传递数据和链接类/方法.

// AppLocationListener class
import java.util.ArrayList;
...

public class AppLocationListener implements LocationListener {
public GPSSensorActivity parentActivity;

public ArrayList<GeoPoint> pointList;

public void onLocationChanged(Location location) {

}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}

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

}

}

// QuestionSearch class
import java.util.ArrayList;
...

public class QuestionSearch extends Activity {

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 5000; // in Milliseconds
private LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizmap);

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

AppLocationListener searchLL = new AppLocationListener();
searchLL.parentActivity = this;

ArrayList<GeoPoint> pointList = new ArrayList<GeoPoint>();
...
// add these points to the list
pointList.add(gMapPoint);
pointList.add(gMapPoint2);
pointList.add(gMapPoint3);


// now set up the location manager and listener
searchLL.pointList = pointList;
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATE,
MINIMUM_DISTANCECHANGE_FOR_UPDATE,
searchLL
);
}
}

最佳答案

为了能够在 searchLL.parentActivity = this; 中传递 this,您的 QuestionSearch 应该是 GPSSensorActivity 或应该扩展它。

Is this because it's not possible to access classes that have parent activities?

不,这与此无关,这是一个纯粹的 Java 问题 - 只需在需要的地方传递正确的类即可。

关于java - 安卓java : accessing class that has parentActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29583036/

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