Android:无法在吐司中查看我的数据

时间:2013-01-26 01:18:13

标签: android arraylist android-arrayadapter toast

为什么我无法在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);

    }
}

MyLog档案:

{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

3 个答案:

答案 0 :(得分:1)

在onCreate中未初始化位置,因此您将获得空指针异常。您应该在使用它之前对其进行初始化

答案 1 :(得分:0)

没有看到崩溃转储是一个猜测游戏。当您尝试将数据显示在吐司中时,我认为locationssnull。因此,当您调用locationss.getData()时,您会收到NullReferenceException。

答案 2 :(得分:0)

可能您遇到了calling populate inappropriately问题。只是从其他问题的答案摘要中,您应该在填充任何数据之前在ItemizedOverlay中调用populate()