开发者

android passing a variable to another java file .... not working

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(latlong开发者_运维问答info);
        setContentView(tv);
 }
}


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


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.


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= "";
         }    
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜