的setText不在的ViewGroup的TextView的是,在服务

问题描述:

这个代码的setText不在的ViewGroup的TextView的是,在服务

... 

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

我创建了我的代码视图,而不参照先前创建的布局引用XML工作,它工作时,我setText。但是,参考布局后,setText无法正常工作。奇怪的是,日志中的getText().toString()已正确写入日志。我不知道它错在哪里。 有什么我做错了吗? 和任何错过? 请告诉我。谢谢。

+0

你应该申报图被替换。 viewgroup.findViewById(R.id.textviewID); –

+0

抱歉! 我错过了代码。 包含在原始来源中。 这是纠正.. –

+0

请不要重复提问。只需使用任何新信息编辑原始帖子,您尝试过的任何新代码或解释为什么发布的答案无效,都会将其置于活动队列的顶部。 –

下面的行:

layoutInflater.inflate(R.layout.service_bike_value, null); 

是创建一个新的布局,但作为第二参数传递的null表示的视图不附着到视图层级结构,这意味着参考它基本上是毫无价值的。

假设你在一个活动中,应该没有必要在调用setContentView之后手动膨胀布局。如果您确实需要保持充气,更改为以下:

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

通过传递空你造成充气常规错过一些重要的步骤,所以应尽量避免,即使你不想立即装上意见(即适配器)。第三个(可选)参数决定是否立即将视图添加到层​​次结构中。

作为最低

linearLayoutPopup = new LinearLayout(this); 

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

否则你创建的对象,设置的东西,然后替换它

+0

你的建议对我很有帮助。 谢谢。祝你好运。 –

+0

我宁愿接受我的回答也不如运气;) –