0

how can i pass latLongString to elhActivity and show it on the screen....both java files are under same package com.elh.whereami;

i used putExtra and getExtars with intent and still nothing is showing on the screen

this is the whereami.java code

package com.elh.database;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
//import android.widget.TextView;

public class whereami extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String provider = locationManager.getBestProvider(criteria, true);

        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);

        locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

    public void updateWithNewLocation(Location location) {



        String latLongString;
        String addressString = "No address found";

        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
            latLongString = "No location found";
        }

        Intent intent = new Intent(this, elhActivity.class);
        intent.putExtra("the_latLongString", latLongString);
        startActivity(intent);

} }

and this is the elhActivity.java

package com.elh.database; 
import android.app.Activity;
import android.widget.TextView;

public class elhActivity extends Activity {
    /** Called when the activity is first created. */
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String latlonginfo = getIntent().getStringExtra("the_latLongString");
        TextView tv = new TextView(this);
        tv.setText(latlonginfo);
        setContentView(tv);
 }
}
2
  • are you getting "No location found" all the time or is it showing nothing i.e empty string ? Commented Mar 21, 2011 at 4:41
  • im getting Hello World, elhAtivity! Commented Mar 21, 2011 at 5:36

3 Answers 3

1

getIntent().getExtra() will return a Bundle object which contains the objects you place using putExtra().

For example:

Bundle arguments = getIntent().getExtras();
String latlonginfo = arguments.getString("the_latLongString");
TextView tv = new TextView(this);
tv.setText(latlonginfo);
setContentView(tv);
Sign up to request clarification or add additional context in comments.

2 Comments

I just realized you are calling setContentView twice. Remove setContentView(tv) and instead, add a TextView to your main.xml layout file and assign it an ID of myTextView. Then, change: TextView tv = new TextView(this); to: TextView tv = (TextView) findViewById(R.id.myTextView);
Also, provide a stack trace via pastebin if it crashes.
0
Intent myIntent = this.getIntent();

String latlonginfo = myIntent.getStringExtra("the_latLongString");

The problem might be you are using setContentView twice in your code.

 setContentView(R.layout.main);
 setContentView(tv);

You will probably have to do away with the 1st setContentView. or better still add a TextView in the layout in design time and intialize your textview with latlonginfo.

2 Comments

i removed the setContent in the whereami.java code i dont need it and i get on the screen is Hello World, elhActivity! so that means i think that im getting the reading from elhActivity but im not reading the latLongString from the whereami.java or its not passed to elhActivity....
You are getting "Hello World, elhActivity!" because the setContentView sets a layout called main.xml to your Activity. main.xml probably has a TextView which is assigned to the String "Hello World, elhActivity!". Try adding a textview to your layout and initialize it with your value. If you still cannot get the value it means that your varaible is getting initialized with an empty string.
0

Consider creating a separate XML layout, say child_dialog.xml for the second activity.

public class ChildActivity extends Activity {
    private String pushedValue;

    @Override
    protected void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.child_dialog);
        try {
              pushValue= getIntent().getExtras().getString("the_latLongString");
         }
         catch(Exception e){
              pushValue= "";
         }    
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.