android java: 访问具有 parentActivity 的 class
android java: accessing class that has parentActivity
我有一个 GPS 侦听器 class AppLocationListener
,它有一个父 activity GPSSensorActivity
(下面未显示)。
我正在尝试使用 AppLocationListener searchLL = new AppLocationListener(); searchLL.parentActivity = this;
从另一个 class QuestionSearch
访问 AppLocationListener
但使用 this
会导致 [=17] 之间的类型不匹配=] 和 GPSSensorActivity
。 .
这是因为无法访问具有父活动的 classes 吗?如何访问 AppLocationListener
。 QuestionSearch
是我添加到现有代码的东西,它不是我的原始代码,我正在尝试我是 java 的新手并且不完全理解传递数据和链接 classes/methods 还没有。
// 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 问题 - 只需向右 类 传递他们期望的地方。
我有一个 GPS 侦听器 class AppLocationListener
,它有一个父 activity GPSSensorActivity
(下面未显示)。
我正在尝试使用 AppLocationListener searchLL = new AppLocationListener(); searchLL.parentActivity = this;
从另一个 class QuestionSearch
访问 AppLocationListener
但使用 this
会导致 [=17] 之间的类型不匹配=] 和 GPSSensorActivity
。 .
这是因为无法访问具有父活动的 classes 吗?如何访问 AppLocationListener
。 QuestionSearch
是我添加到现有代码的东西,它不是我的原始代码,我正在尝试我是 java 的新手并且不完全理解传递数据和链接 classes/methods 还没有。
// 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 问题 - 只需向右 类 传递他们期望的地方。