简体   繁体   English

Android Java:从另一个类调用方法会导致致命异常

[英]Android java: Calling method from another class causes fatal exception

I have an class QuestionSearch extended from GPSSensorActivity . 我有一个从GPSSensorActivity扩展的QuestionSearch类。 I want to call QuestionSearch.testLocation() from another class QuizMap . 我想从另一个类QuizMap调用QuestionSearch.testLocation() On the call I get a nullPointerException fatal exception and I can't work out why. 在通话中,我收到一个nullPointerException致命异常,但我不知道为什么。 It is related to QuestionSearch.testLocation() call though: LogCat lists it as an error and the app functions when call to QuestionSearch.testLocation() commented out. 不过,它与QuestionSearch.testLocation()调用有关:LogCat将其列为错误,并且当对QuestionSearch.testLocation()调用被注释掉时,应用程序会运行。

I think it's also related to making the call from onPostExecute and there's an SO post here that suggests the correct approach but I can't quite get my head around it (still new to java). 我想,这也是关系到使从onPostExecute呼叫,并有一个SO张贴在这里它代表着在正确的做法,但我不能完全得到我的头周围(还是新的Java)。

Code snippets below, think I've included enough. 下面的代码段,认为我已经足够了。

// 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();

Stack trace here: 堆栈跟踪在这里:

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)

The location manager is still running and giving updates. 位置管理器仍在运行并提供更新。 I noticed that I'd called locationUpdated twice, once in QuestionSearch and once earlier in GPSSensorActivity . 我注意到我曾两次致电locationUpdated ,一次是在QuestionSearch ,一次是在GPSSensorActivity There's no need to do this so i dispensed with the QuetionSearch instance of locationUpdater'. 不需要这样做,所以QuetionSearchlocationUpdater'.QuetionSearch实例locationUpdater'. nullPointerException` has gone now, to be replaced by another exception. nullPointerException`现在已经消失了,将由另一个异常替换。 I'll raise a different SO question for that. 为此,我将提出另一个不同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM