0

为什么我在 Toast 中看不到我的数据?每次我运行该应用程序时,它都会崩溃。但是,如果我删除 Toast 它运行顺利。

到目前为止,这是我的代码,我已经删除了所有不相关的代码

GoogleMaps Class.

    public class GoogleMaps extends MapActivity implements LocationListener {



    public void addLocation() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        alert.setTitle("What do you want to call the location?");
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                value = input.getText().toString().trim();
                checklocationTitle();
            }
        });

        alert.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
        alert.show();
    }


    public void checklocationTitle() {
        if (value.length() > 3) {
            Toast.makeText(this, "Name of the locations is know " + value,
                    Toast.LENGTH_LONG).show();
            try {
                markedpinpoint = true;
                midllat = touchedPoint.getLatitudeE6() / 1E6;
                midlongi = touchedPoint.getLongitudeE6() / 1E6;
                Geocoder geocoder = new Geocoder(getBaseContext(),
                        Locale.getDefault());
                List<Address> adress = geocoder.getFromLocation(
                        touchedPoint.getLatitudeE6() / 1E6,
                        touchedPoint.getLongitudeE6() / 1E6, 1);
                if (adress.size() > 0) {
                    String display = "";
                    for (int i = 0; i < adress.get(0).getMaxAddressLineIndex(); i++) {
                        display += adress.get(0).getAddressLine(i) + "\n";
                        OverlayItem overlayitem = new OverlayItem(touchedPoint,
                                value, display);
                        custom = new Location_Service(d, GoogleMaps.this);
                        custom.insertLocation(overlayitem);
                        overlayList.add(custom);
                    }
                } else {
                    Toast.makeText(
                            this,
                            "There where a problem to locate the selected adresse",
                            Toast.LENGTH_LONG).show();
                }
            } catch (IOException e) {
            }
        } else {
            Toast.makeText(this,
                    "Please provide a least 3 cifre Title for your location.",
                    Toast.LENGTH_LONG).show();
            addLocation();
        }
    }


    public void buttonLocations(View view) {
        // stopLocationListner();
        // stopBackgroundService();
        Intent intent = new Intent(this, PinPoints.class);
        startActivity(intent);
        // Toast.makeText(this, "Gemte steder: " + custom.size(),
        // Toast.LENGTH_LONG).show();

    }
}

    public class Location_Service extends ItemizedOverlay<OverlayItem> {

        public ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();

        public Location_Service(Drawable defaultMarker) {
            super(boundCenter(defaultMarker));
            // TODO Auto-generated constructor stub
        }

        public ArrayList<Locations> getData() {
            Locations hej = new Locations();
            ArrayList<Locations> tt = new ArrayList<Locations>();
            for (OverlayItem test : pinpoints) {
                hej.setAdress(test.getSnippet());
                hej.setMidlat(test.getPoint().getLatitudeE6());
                hej.setMidlong(test.getPoint().getLongitudeE6());
                hej.setTitle(test.getTitle());
                tt.add(hej);
            }
            return tt;
        }

        public Location_Service(Drawable m, Context context) {
            this(m);
        }

        @Override
        protected OverlayItem createItem(int i) {
            return pinpoints.get(i);
        }

        @Override
        public int size() {
            return pinpoints.size();
        }

        public void insertLocation(OverlayItem item) {
            pinpoints.add(item);
            this.populate();
        }

    }


public class Locations {
    int midlat;
    int midlong;
    String title;
    String adress;
    String distance;

    public Locations(String title, String adress, String distance) {
        super();
        this.title = title;
        this.adress = adress;
        this.distance = distance;
    }

    public Locations() {

    }

    public int getMidlat() {
        return midlat;
    }

    public String getDistance() {
        return distance;
    }

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

    public void setMidlat(int i) {
        this.midlat = i;
    }

    public int getMidlong() {
        return midlong;
    }

    public void setMidlong(int midlong) {
        this.midlong = midlong;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return title + adress + distance;
    }

}

问题出现在 Toast 中...

public class PinPoints extends Activity

{

    private ListView lv;
    private LocationsAdapter adapter;
    private Location_Service locationss;
    private ArrayList<Locations> fetch = new ArrayList<Locations>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locations);

        Toast.makeText(this,
                "Places: " + locationss.getData(),
                Toast.LENGTH_LONG).show();

        for (int i = 0; i < 5; i++) {

            Locations t = new Locations("Home", "Edvard", "2200m");

            fetch.add(t);
        }

        lv = (ListView) findViewById(R.id.listView1);

        adapter = new LocationsAdapter(PinPoints.this, R.id.listView1, fetch);
        lv.setAdapter(adapter);

    }
}

我的日志文件:

{com.example.test/com.example.test.PinPoints}: java.lang.NullPointerException
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.os.Looper.loop(Looper.java:137)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at java.lang.reflect.Method.invoke(Method.java:511)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at dalvik.system.NativeStart.main(Native Method)
01-26 02:27:17.900: E/AndroidRuntime(19389): Caused by: java.lang.NullPointerException
01-26 02:27:17.900: E/AndroidRuntime(19389):    at com.example.test.PinPoints.onCreate(PinPoints.java:31)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.Activity.performCreate(Activity.java:5206)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-26 02:27:17.900: E/AndroidRuntime(19389):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-26 02:27:17.900: E/AndroidRuntime(19389):    ... 11 more
4

3 回答 3

1

Locationss 没有在 onCreate 中初始化,所以你得到一个空指针异常。您应该在使用它之前对其进行初始化

于 2013-01-26T01:21:15.873 回答
0

可能,您遇到了调用填充不当的问题。只是在填充任何数据之前,您应该在 ItemizedOverlay 中调用 populate()的另一个问题的答案的摘录

于 2013-01-26T01:45:39.800 回答
0

没有看到崩溃转储有点猜谜游戏。我认为locationssnull当您尝试让数据显示在吐司中时。所以当你打电话时,locationss.getData()你会得到一个 NullReferenceException。

于 2013-01-26T01:22:29.847 回答