简体   繁体   English

TextView.setText()不起作用

[英]TextView.setText() not working

I have this piece of code... 我有这段代码...

class IncomingHandler extends Handler
{
    @Override
    public void handleMessage(Message message) 
    {

        String totalReceived = (String) message.obj;
        Log.i("TAG", "total received: " + totalReceived);
        totalTextView.setText("" + totalReceived);

        Log.i("TAG", (Looper.getMainLooper().getThread() == Thread.currentThread()) ? "UI thread" : "NOT UI thread");
        //Toast.makeText(MainActivity.this, "message received.", Toast.LENGTH_LONG).show();

    };
};

I run my app and it works just fine, but if i recreate the activity, for instance by changing the device orientation, the text will not be updated. 我运行我的应用程序,并且运行良好,但是如果我重新创建活动(例如通过更改设备方向),则不会更新文本。 Note that i do receive the messages and they are successfully printed by LogCat. 请注意,我确实收到了消息,并且LogCat已成功打印了消息。

Also note that on my last log i try to determine if I am running on the main thread. 还要注意,在我的上一个日志中,我尝试确定我是否在主线程上运行。 If that check is correct, I am indeed running on the UI thread... 如果检查正确,那么我确实在UI线程上运行...

Any ideas on what i might be doing wrong? 对我可能做错的任何想法?

Cheers, Alex 干杯,亚历克斯

正如皮棉建议处理程序应该是静态的,使处理程序成为静态,并创建对活动的weakReference ,然后通过活动引用访问textview,我认为它应该可以工作

try to save instance add this 尝试保存实例添加此

@Override
protected void onSaveInstanceState(Bundle outState) {
State s = new State(yourTextView.getText().toString());
outState.putSerializable(State.STATE, s);
super.onSaveInstanceState(outState);
 }
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
State s = (State) savedInstanceState.getSerializable(State.STATE);
yourTextView.setText(s.getYourTextViewText());
 }

Your problem is that totalTextView is still pointing to the TextView of the (now destroyed) previous activity. 您的问题是totalTextView仍指向先前活动(现已销毁)的TextView。

If class IncomingHandler is a sub-class of Activity, it should be an easy job to make sure that during onCreate() you make sure to update it with totalTextView = (TextView)findViewById(R.id.__/* something */__); 如果class IncomingHandler类是Activity的子类,那么确保在onCreate()期间确保使用totalTextView = (TextView)findViewById(R.id.__/* something */__);来更新它应该是一件容易的totalTextView = (TextView)findViewById(R.id.__/* something */__);

If the handler is not a sub-class of Activity, well, maybe it should be, or you should look into some more Android-Framework-High-Level stuff to update and call back the Activity (eg Loaders or UI-less fragments with setRetainInstance(true); ) 如果处理程序不是Activity的子类,也许应该是,或者您应该研究更多Android-Framework-High-Level的东西来更新和回调Activity(例如,加载程序或带有UI的无片段) setRetainInstance(true);

ps.: some users will tell you to just override the destruction of the Activity by putting configChanged in the manifest. ps .:某些用户会告诉您只需将configChanged放在清单中即可覆盖对Activity的破坏。 Although it might work at first moment, it's a poor quick fix, it's an unadvisable pattern that usually will lead to bigger problems in the future. 尽管它可能在一开始就起作用,但是它是一个较差的快速解决方案,它是一种不建议使用的模式,通常会在将来导致更大的问题。

from: http://developer.android.com/guide/topics/manifest/activity-element.html#config 来自: http : //developer.android.com/guide/topics/manifest/activity-element.html#config

Note: Using this attribute should be avoided and used only as a last-resort. 注意:应避免使用此属性,并且只能将其用作最后一种手段。 Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change. 请阅读处理运行时更改以获取有关如何正确处理由于配置更改而导致的重新启动的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM