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

riceHallですnull。初期化したことがないためです。

于 2013-04-20T20:29:37.370 に答える
1

変数riceHallが null のようです。の実装を切断したためmakeHistoricalLocationsわかりませんが、そのメソッドを呼び出していないか、(より可能性が高い) のメソッドローカル バージョンを作成riceHallしてオブジェクト レベルで設定していない可能性があります。

于 2013-04-20T20:29:58.280 に答える