我从QuestionSearch
扩展了一个班级GPSSensorActivity
。我想从另一个班级QuestionSearch.testLocation()
拨打QuizMap
。在调用时,我得到一个nullPointerException致命异常,我无法解决原因。它与QuestionSearch.testLocation()
调用有关:LogCat将其列为错误,并且在调用QuestionSearch.testLocation()
时注释掉应用程序的功能。
我认为这也与从onPostExecute进行调用有关,并且有一个SO帖子here表明了正确的方法,但我无法理解它(对java来说还是新手)。
下面的代码片段,认为我已经包含了足够的内容。
// GPSSensorActivity class
public class GPSSensorActivity extends Activity {
private static final String TAG = "GPSSensorActivity"; // debugging tag
private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 5000; // in Milliseconds
// these strings are used when saving the users' preferred location
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationUpdater();
}
public void locationUpdater() {
// set up a new location manager
// this controls the location services on the phone
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// set up a listener from AppLocationListener class to listen for changes in location
AppLocationListener androidLL = new AppLocationListener();
androidLL.parentActivity = this;
// get location updates
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATE,
MINIMUM_DISTANCECHANGE_FOR_UPDATE,
androidLL);
}
// QuestionSearch class
public class QuestionSearch extends GPSSensorActivity {
private static final String TAG = "QuestionSearch"; // debugging tag
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizmap);
locationUpdater();
}
public void testLocation() {
Location location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(TAG, "latitude is " + location.getLatitude());
}
// QuizMap class that tries to call QuestionSearch
public class QuizMap extends FragmentActivity
implements OnMarkerClickListener {
private GoogleMap map;
private static final String TAG = "QuizMap"; // debugging
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
protected void onPostExecute(String result) {
// create the various points on the map
ParseKMLAndMap dm = new ParseKMLAndMap();
dm.mapView = map;
dm.resultString = result;
//Log.i(TAG,"DownloadWebPageTask result" + result); //debugging
dm.startParsing();
startQuestionSearch(result);
}
}
public void startQuestionSearch(String result) {
QuestionSearch questionSearch = new QuestionSearch();
questionSearch.testLocation();
此处的堆栈跟踪:
04-12 12:51:15.511: E/AndroidRuntime(31797): FATAL EXCEPTION: main
04-12 12:51:15.511: E/AndroidRuntime(31797): java.lang.NullPointerException
04-12 12:51:15.511: E/AndroidRuntime(31797): at uk.ac.ucl.cege.cegeg077.ucesrud.GeoQuiz.QuestionSearch.testLocation(QuestionSearch.java:42)
04-12 12:51:15.511: E/AndroidRuntime(31797): at uk.ac.ucl.cege.cegeg077.ucesrud.GeoQuiz.QuizMap.startQuestionSearch(QuizMap.java:198)
04-12 12:51:15.511: E/AndroidRuntime(31797): at uk.ac.ucl.cege.cegeg077.ucesrud.GeoQuiz.QuizMap$DownloadWebPageTask.onPostExecute(QuizMap.java:167)
04-12 12:51:15.511: E/AndroidRuntime(31797): at uk.ac.ucl.cege.cegeg077.ucesrud.GeoQuiz.QuizMap$DownloadWebPageTask.onPostExecute(QuizMap.java:1)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.os.AsyncTask.finish(AsyncTask.java:417)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.os.Looper.loop(Looper.java:130)
04-12 12:51:15.511: E/AndroidRuntime(31797): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-12 12:51:15.511: E/AndroidRuntime(31797): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 12:51:15.511: E/AndroidRuntime(31797): at java.lang.reflect.Method.invoke(Method.java:507)
04-12 12:51:15.511: E/AndroidRuntime(31797): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-12 12:51:15.511: E/AndroidRuntime(31797): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-12 12:51:15.511: E/AndroidRuntime(31797): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
位置管理器仍在运行并提供更新。我注意到我已经locationUpdated
两次调用,一次在QuestionSearch
,一次在GPSSensorActivity
。没有必要这样做,所以我免除QuetionSearch
nullPointerException的locationUpdater'.
实例现在已经消失,被另一个异常取代。我会为此提出一个不同的SO问题。