应用程序因某种原因崩溃:返回并分配问题

时间:2011-12-20 12:39:51

标签: android location return assign

在我的Main类中,我调用另一个类来执行某些操作,我需要将数据返回给Main类,但是在启动应用程序时它因某些原因而崩溃:

我的Main活动 - 它在getLocation()中崩溃,与返回位置和分配到Point有关,因为当我将这些线路取出时,它可以正常工作。

public class Main extends Activity {

    ArrayList<Point> gpsCoordinates = new ArrayList<Point>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pages);
        vf = (ViewFlipper) findViewById(R.id.viewFlipper);

        try {
            getLocation(null);
        } catch (InterruptedException e) {}

    }

public synchronized void getLocation(View view) throws InterruptedException
    {

        LocationHandler lh = new LocationHandler();
        lh.getLocation(view,this);
        Location location = new Location(lh.returnLocation());
        Point point = new Point(location.getLongitude(),location.getLatitude());
        gpsCoordinates.add(point);
    } 
}

My Point课程:

public class Point {

    private double x;
    private double y;

    public Point(double x, double y) {
        this.setX(x);
        this.setY(y);
    }

    public Point(Point point) {
        this.setX(point.getX());
        this.setY(point.getY());
    }

    public String toString(Point point)
    {
        String stringPoint = "";
        stringPoint = "(" + point.getX() + "," + point.getY() + ")";
        return stringPoint;
    }

    public void setX(double x2) {
        this.x = x2;
    }

    public double getX() {
        return this.x;
    }

    public void setY(double y2) {
        this.y = y2;
    }

    public double getY() {
        return this.y;
    }
}

我正在从Main打电话:

public class LocationHandler {
    Location location;
    public synchronized void getLocation(final View view, final Main main) throws InterruptedException
    {   

        Thread t = new Thread(){
            public synchronized void run(){

                location = new Location(mlocListener.returnLocation());


            }

        };
        t.start();

    } 

    public Location returnLocation()
    {   
        return location;

    }

}

错误日志:

12-20 12:35:11.348: E/AndroidRuntime(3466): FATAL EXCEPTION: main
12-20 12:35:11.348: E/AndroidRuntime(3466): java.lang.RuntimeException: Unable to start activity ComponentInfo{mfc.generalgui6api8/mfc.generalgui6api8.Main}: java.lang.NullPointerException
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.os.Looper.loop(Looper.java:123)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at java.lang.reflect.Method.invoke(Method.java:521)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at dalvik.system.NativeStart.main(Native Method)
12-20 12:35:11.348: E/AndroidRuntime(3466): Caused by: java.lang.NullPointerException
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.location.Location.set(Location.java:123)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.location.Location.<init>(Location.java:116)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at mfc.generalgui6api8.Main.getLocation(Main.java:133)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at mfc.generalgui6api8.Main.onCreate(Main.java:44)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-20 12:35:11.348: E/AndroidRuntime(3466):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-20 12:35:11.348: E/AndroidRuntime(3466):     ... 11 more

1 个答案:

答案 0 :(得分:0)

我最终使用了:

How to declare global variables in Android?

这使我可以在LocationListener

中检索GPS位置时直接保存GPS位置