Android setText在引用服务中xml的视图组的TextView中不起作用

Android setText在引用服务中xml的视图组的TextView中不起作用,android,service,viewgroup,rootview,Android,Service,Viewgroup,Rootview,此代码 ... private LayoutInflater layoutInflater; private ViewGroup rootView; int wrap_content = WindowManager.LayoutParams.WRAP_CONTENT; ... linearLayoutPopup = new LinearLayout(this); linearLayoutPopup.setBackgroundColor(getResources

此代码

...

private LayoutInflater layoutInflater;
private ViewGroup rootView;
int wrap_content = WindowManager.LayoutParams.WRAP_CONTENT;

...
        linearLayoutPopup = new LinearLayout(this);
        linearLayoutPopup.setBackgroundColor(getResources().getColor(R.color.colorExResult));     
        linearLayoutPopup.setOrientation(LinearLayout.HORIZONTAL);

        layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);


        mParams = new WindowManager.LayoutParams(
                wrap_content,
                wrap_content,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,  
                PixelFormat.TRANSLUCENT); 
        mParams.gravity = Gravity.LEFT | Gravity.TOP; 
        mParams.y = 100;

        rootView = (ViewGroup) layoutInflater.inflate(R.layout.service_bike_value, null);
        linearLayoutPopup = (LinearLayout) layoutInflater.inflate(R.layout.service_bike_value, null);

     if(rootView != null) {
        textView_speed_service = (TextView) rootView.findViewById(R.id.textView_Speed_service);

    }

        timerHandler.sendEmptyMessage(0);
...

public Handler timerHandler = new Handler(){

    public void handleMessage(Message msg)
    {
        textViewspeed.setText(""+speed);

        Log.d("textViewSpeed", textViewspeed.getText().toString());
        timerHandler.sendEmptyMessageDelayed(0, 200); }
    };
我在我的代码中创建了一个视图,没有引用以前创建的布局,当我
setText
时它就工作了。但是,引用布局后,
setText
无法正常工作。奇怪的是,日志中的
getText().toString()
被正确地写入日志。我不知道哪里错了。 我做错什么了吗? 还有什么遗漏的吗? 请告诉我。多谢各位

下面一行:

layoutInflater.inflate(R.layout.service_bike_value, null);
正在创建新布局,但作为第二个参数传递的null表示该视图未附加到视图层次结构,这意味着对它的引用基本上毫无价值

假设您处于活动中,则在调用setContentView后,不需要手动膨胀布局。如果确实需要保持充气状态,请更改为以下步骤:

layoutInflater.inflate(R.layout.service_bike_value, rootView, true);
通过传递null,您会导致充气例程错过一些重要步骤,因此即使您不想立即附加视图(即适配器),也应该避免。第三个(可选)参数决定是否立即将视图添加到层次结构中

至少

linearLayoutPopup = new LinearLayout(this);
应替换为

linearLayoutPopup = (LinearLayout) layoutInflater.inflate(R.layout.service_bike_value, rootView, true);

否则,您将创建对象、设置对象,然后替换它

您应该声明视图。findViewById(R.id.textviewID);哎呀,对不起!我错漏了密码。包含在原始来源中。已经更正了,请不要重复问题。简单地用您拥有的任何新信息、您尝试过的任何新代码编辑您的原始帖子,或者解释任何帖子答案不起作用的原因,都会将其推到活动队列的顶部。抱歉,我不知道。我会提前小心的。你的建议对我很有帮助。非常感谢。祝你好运。我宁愿接受我的答案,也不愿碰运气;)