Zaz
Zaz

Reputation: 1102

Android: Cant see my data in toast

Why cant i see my data in my Toast?? Every time i run the app, it crash. But if i delete the Toast it runs smoothly.

This is my code so far, i have strip all the irrelevant code

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;
    }

}

The problem occurs here in the 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 File:

{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

Upvotes: 0

Views: 392

Answers (3)

PCoder
PCoder

Reputation: 2185

Probably, you are running into the calling populate inappropriately problem. Just an extract of the answer from the other question you should call populate() in your ItemizedOverlay before any data is populated

Upvotes: 0

frozenkoi
frozenkoi

Reputation: 3248

Without seeing the crash dump is a bit of a guessing game. I think locationss is null when you try to get the data to show in the toast. So when you call locationss.getData() you get a NullReferenceException.

Upvotes: 0

fedepaol
fedepaol

Reputation: 6862

Locationss is not initialized in onCreate, so you are getting a null pointer excepiton. You should initialize it before using it

Upvotes: 1

Related Questions