安卓:findViewById给了我错了指针? [英] Android: findViewById gives me wrong pointer?

查看:248
本文介绍了安卓:findViewById给了我错了指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我updateGUI()函数,我每次更新TextView的,像这样:

In my updateGUI() function I update each TextView like so:

((TextView) findViewById(R.id.lbTrackerLat)).setText(String.valueOf(lat));
((TextView) findViewById(R.id.lbTrackerLong)).setText(String.valueOf(lng));
((TextView) findViewById(R.id.lbTrackerAlt)).setText(String.valueOf(alt));

当我从在OnCreate的onclick分配一个按钮preSS调用updateGUI(),更新工作ALLWAYS

When I call updateGUI() from the press of a button with the onclick assigned in the onCreate, the update ALLWAYS works.

我有当用户点击另一个按钮的启动了LocationListener的,并在每次更新时我叫updateGUI()有作为​​,其工作,直到屏幕第一次转动,之后屏幕不刷新的某些原因。

I have a locationListener that's started when the user clicks another button, and on every update event I call updateGUI() there as well, which works until the screen is first rotated, after which the screen is not refreshed for some reason.

在试图找出原因,我已经添加了这code到updateGUI()函数:

While trying to figure out why, I've added this code to the updateGUI() function:

TextView t = (TextView) findViewById(R.id.lbTrackerPostLastRefresh);
t.setText(formattedDate);
Log.d("H","EXIT  lbTrackerPostLastRefresh="+t.toString());

旋转之后(的onCreate再次调用),当它从按钮点击叫,我得到44c76a78​​作为指针。当LocationListener的更新函数调用findVIewByID,指针44c1b518返回!! ??

After rotation (onCreate is called again), when it's called from the button click, I get 44c76a78 as the pointer. When the locationListener update function calls findVIewByID, the pointer 44c1b518 is returned!!??

我得到的布局破坏并重新创建的,但为什么findViewById返回旧指针值??

I get that the layout is destroyed and recreated, but why is findViewById returning old pointer values??

Spitballing:
这是因为如果该视图的开始与最初的onCreate一审即使在一个新的查看该设备已在旋转时被实例化继续上欢快地(与它的资源一起)。也许是因为我一直没有停止过它的LocationListener的是这样做的。但停药,每一个设备旋转时开始似乎傻了!

Spitballing: It's as if the first instance of the View starting with the initial onCreate continues merrily on (along with it's resources) even after a new View as been instantiated when the device was rotated. Perhaps the LocationListener is doing this because I haven't stopped it. But stopping and starting it every time the device is rotated seems stupid!

推荐答案

我猜测LocationListener的是一个匿名内部类的活动,这意味着它保存了它的父类的引用。所以,现在,如果这是LocationListener的的的中的onCreate()调用后重新创建,它会引用旧活动,并findViewById()的调用将返回对象由过去的活动举行。这不仅会导致程序无法正确执行,但导致内存泄漏以及(基本上是原来的活动创造一切都将被保留)。

I am guessing that locationListener is an anonymous inner class of an Activity, which means that it holds a reference to its parent class. So now if this locationListener is not recreated after the onCreate() call, it will reference the old Activity, and a call to findViewById() will return objects hold by that old Activity. Not only will this cause your program to perform incorrectly, but causing memory leaks as well (basically everything created by the original Activity will be retained).

如果上面是不是原因,那我就需要更多的信息,关于LocationListener的以及它是如何为了帮助使用。

If the above is not the cause, then I'll need more information regarding locationListener and how it is used in order to help.

这篇关于安卓:findViewById给了我错了指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆