您好,我的应用程序出现问题。当我为 Android 2.2 或更低版本创建新项目时,我的应用程序工作正常,并且我的 toast 显示在屏幕上,但是当我使用相同的代码为 2.3 或 2.3.3 创建新项目时,toast 根本不显示。另外,我在主 OnCreate 线程中添加了 Textview 更新,但我的 textview 仍然没有更新。我主要需要解决 toast 问题。 谢谢
public class Location extends Activity {
/** Called when the activity is first created. */
static String Text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final TextView tv = (TextView)findViewById(R.id.textView1);
lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
private final String TAG = null;
@Override
public void onLocationChanged(android.location.Location location) {
// TODO Auto-generated method stub
Text = "My current location is: " + "Latitud = " + location.getLatitude() + "Longitud = " + location.getLongitude();
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, Text, duration).show();
tv.setText(Text);
try
{
File root = Environment.getExternalStorageDirectory();
File gps = new File(root, "log.txt");
BufferedWriter out = new BufferedWriter(
new FileWriter(gps,true)) ;
out.write(Text);
out.write("");
out.close();
}
catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
}
}
最佳答案
您必须在 Toast 上调用 show()。
Toast.makeText(context, Text, duration).show();
您所做的只是创建 Toast,而不是显示它。
TextView 更新可能会因异常而失败。
关于android - Android 2.3.3 上的 Toast 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5342672/