0

我使用吸气剂的方式和我一直做的一样(我认为),它使程序崩溃,但我不知道为什么!有谁知道我的错误在哪里?

它在下面的第 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();

日志猫:

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
4

3 回答 3

4

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

public void makeHistoricalLocations() {
    this.riceHall = new HistoricalLocation(
    ...
于 2013-04-20T20:30:11.823 回答
1

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

于 2013-04-20T20:29:37.370 回答
1

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

于 2013-04-20T20:29:58.280 回答