Getter正在崩溃程序

时间:2013-04-20 20:27:14

标签: java android crash getter

我使用吸气剂的方式与我一直以来一样(我认为),它会使程序崩溃,但我无法弄清楚原因!有谁知道我的错误在哪里?

下面第45行崩溃:

package edu.virginia.hotncolduva;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import sofia.app.Screen;
import android.content.Context;
import android.location.LocationManager;
import android.os.SystemClock;
import android.text.Editable;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;

public class HotNColdUva extends Screen {
    private TextView txtHotOrCold;
    private TextView txtLocation;
    private double currentLatitude;
    private double currentLongitude;
    private static List<HistoricalLocation> places;
    private static Chronometer chronometer;
    private boolean victor;
    private EditText inputLatitude;
    private EditText inputLongitude;

    private HistoricalLocation riceHall;

    public void initialize() {

        presentScreen(MainMenu.class);
        presentScreen(Splash.class);

        victor = false;

        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();
        makeHistoricalLocations();
        for(HistoricalLocation hl : places){
            hl.setDistance(howFar(hl));
        }

        //THIS IS WHAT CAUSES THE CRASH
        riceHall.getLatitude();

        currentLatitude = 38.03211;
        currentLongitude = -78.51002;

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

        // LocationListener locList = new WhereAmI();
        // lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        // locList);

        // new Thread(new Runnable(){
        // public void run(){

        // Timer.callRepeatedly(this, "update", 1000);
        // }
        // }).start();

        // jeffersonStatue();
        new Timer().schedule(new TimerTask() {
            public void run() {
                update();
            }
        }, 1000, 1000);
    }

    public void btnTeleportClicked(){


//      currentLatitude = doubleinputLatitude.getText();
//      currentLongitude = inputLatitude.getText();;

    }

    public void makeHistoricalLocations() {
        HistoricalLocation riceHall = new HistoricalLocation(
                "Rice Hall",
                38.03161,
                -78.51075,
                0,
                "\"The real promise of what will happen here is the 21st Century extension of the academical village and what will be made possible by the technologies that are used, developed and explored in these places. The village won’t simply exist then in these buildings or on this campus, but across the state, across the nation and globe. We have only really begun to understand the way in which these technologies can enhance human performance and accomplishment and how they can improve the human condition.\"\n\n-Paul Rice",
                false);}

package edu.virginia.hotncolduva;

import sofia.app.Screen;

public class HistoricalLocation extends Screen {
    private String name;
    private double latitude;
    private double longitude;
    private double distance;
    private String hist;
    private boolean visited;



    public HistoricalLocation(String name, double latitude, double longitude, double distance,
            String hist, boolean visited) {
        super();
        this.name = name;
        this.latitude = latitude;
        this.longitude = longitude;
        this.distance = distance;
        this.hist = hist;
        this.visited = visited;
    }


    @Override
    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getDistance() {
        return distance;
    }


    public void setDistance(double distance) {
        this.distance = distance;
    }


    public String getHist() {
        return hist;
    }


    public boolean getVisited() {
        return visited;
    }


    public void hasVisited() {
        visited = true;
    }




}

这显然不是整个项目,但这是所有正在使用的代码。它崩溃了

    riceHall.getLatitude();

logcat的:

04-20 16:26:27.700: W/ActivityThread(27796): Application edu.virginia.hotncolduva can be debugged on port 8100...
04-20 16:26:27.760: D/dalvikvm(27796): GC_FOR_ALLOC freed 131K, 4% free 7361K/7640K, paused 17ms, total 19ms
04-20 16:26:27.760: I/dalvikvm-heap(27796): Grow heap (frag case) to 7.842MB for 539664-byte allocation
04-20 16:26:27.790: D/dalvikvm(27796): GC_CONCURRENT freed 1K, 4% free 7886K/8168K, paused 5ms+2ms, total 29ms
04-20 16:26:27.790: D/dalvikvm(27796): WAIT_FOR_CONCURRENT_GC blocked 24ms
04-20 16:26:27.830: D/dalvikvm(27796): GC_FOR_ALLOC freed <1K, 4% free 7886K/8168K, paused 14ms, total 14ms
04-20 16:26:27.830: I/dalvikvm-heap(27796): Grow heap (frag case) to 8.751MB for 955696-byte allocation
04-20 16:26:27.850: D/dalvikvm(27796): GC_FOR_ALLOC freed 0K, 4% free 8819K/9104K, paused 17ms, total 17ms
04-20 16:26:27.870: D/dalvikvm(27796): GC_CONCURRENT freed <1K, 4% free 8820K/9104K, paused 2ms+1ms, total 22ms
04-20 16:26:27.930: D/AndroidRuntime(27796): Shutting down VM
04-20 16:26:27.930: W/dalvikvm(27796): threadid=1: thread exiting with uncaught exception (group=0x40d12930)
04-20 16:26:27.940: E/AndroidRuntime(27796): FATAL EXCEPTION: main
04-20 16:26:27.940: E/AndroidRuntime(27796): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.HotNColdUva}: java.lang.NullPointerException
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.os.Looper.loop(Looper.java:137)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at dalvik.system.NativeStart.main(Native Method)
04-20 16:26:27.940: E/AndroidRuntime(27796): Caused by: java.lang.NullPointerException
04-20 16:26:27.940: E/AndroidRuntime(27796):    at edu.virginia.hotncolduva.HotNColdUva.initialize(HotNColdUva.java:45)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at sofia.internal.events.EventDispatcher$MethodTransformer.invoke(EventDispatcher.java:474)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at sofia.internal.events.EventDispatcher.invokeTransformer(EventDispatcher.java:136)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at sofia.internal.events.EventDispatcher.dispatch(EventDispatcher.java:109)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at sofia.app.internal.ScreenMixin.invokeInitialize(ScreenMixin.java:561)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at sofia.app.Screen.onCreate(Screen.java:186)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.Activity.performCreate(Activity.java:5104)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-20 16:26:27.940: E/AndroidRuntime(27796):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-20 16:26:27.940: E/AndroidRuntime(27796):    ... 11 more
04-20 16:26:27.970: D/dalvikvm(27796): GC_CONCURRENT freed 797K, 11% free 8472K/9416K, paused 2ms+5ms, total 34ms

3 个答案:

答案 0 :(得分:4)

在方法makeHistoricalLocations中,您将新对象的引用分配给局部变量,而不是实例1。

public void makeHistoricalLocations() {
    this.riceHall = new HistoricalLocation(
    ...

答案 1 :(得分:1)

riceHallnull,因为您从未初始化它。

答案 2 :(得分:1)

您的变量riceHall似乎为空。我无法分辨,因为你切断了makeHistoricalLocations的实现,但我怀疑你要么没有调用那个方法,要么(更有可能)创建一个方法本地版本的riceHall而不是设置它对象级别。