2011-03-21 111 views
0

如何传递latLongString到elhActivity并显示在屏幕上....这两个Java文件都是同一个包com.elh.whereami;安卓传递变量到另一个Java文件....不工作

我用putExtra和下意图,仍然没有getExtars正显示在屏幕

本上是whereami.java代码

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

}}

,这是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); 
} 
} 
+0

你是否总是找到“找不到位置”,或者它是否显示空白字符串? – Umesh 2011-03-21 04:41:51

+0

即时获得空白屏幕 – Moe 2011-03-21 05:24:00

+0

即时通讯Hello World,elhAtivity! – Moe 2011-03-21 05:36:52

回答

1

getIntent()。getExtra()将返回包含您用putExtra的对象()一个Bundle对象。

例如:

Bundle arguments = getIntent().getExtras(); 
String latlonginfo = arguments.getString("the_latLongString"); 
TextView tv = new TextView(this); 
tv.setText(latlonginfo); 
setContentView(tv); 
+0

该应用程序崩溃....! – Moe 2011-03-21 06:39:21

+0

我刚刚意识到你正在调用setContentView两次。删除'setContentView(tv)',而是将一个TextView添加到您的main.xml布局文件中,并为其指定一个myTextView的ID。然后,更改: 'TextView tv = new TextView(this);' to: 'TextView tv =(TextView)findViewById(R.id.myTextView);' – Victor 2011-03-21 20:57:09

+0

另外,如果崩溃,则通过pastebin提供堆栈跟踪。 – Victor 2011-03-21 20:58:08

0
Intent myIntent = this.getIntent(); 

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

的问题可能是你在你的代码中使用的setContentView两次。

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

您可能必须删除第1个setContentView。 或更好的还是在设计时的布局中添加一个TextView,并使用latlonginfo初始化您的textview。

+0

我删除了whereami.java代码中的setContent,我不需要它,我在屏幕上显示的是Hello World,elhActivity!所以这意味着我认为即时通讯从elhActivity中读取,但我没有从whereami.java读取latLongString,或者它没有通过elhActivity ... – Moe 2011-03-21 06:38:43

+0

您正在获得“Hello World,elhActivity!”因为setContentView为您的Activity设置了一个名为main.xml的布局。 main.xml可能有一个分配给字符串“Hello World,elhActivity!”的TextView。尝试添加一个textview到你的布局,并用你的价值初始化它。如果仍然无法获得值,则意味着您的变量正在使用空字符串进行初始化。 – Umesh 2011-03-21 07:11:58

0

考虑创建一个单独的XML布局,比如child_dialog.xml用于第二个活动。

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