androidserviceviewgrouprootview

setText does not work in the TextView of the ViewGroup that references xml in the service


this code

...

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

I created a view in my code without referencing the previously created Layout and it worked when I setText. However, setText does not work properly after you reference the layout. Strangely, getText().toString() in Log is properly written to Log. I do not know where it is wrong. Is there anything I have done wrong? and anything missed? Please tell me. Thank you.


Solution

  • The following line:

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

    is creating a new layout but the null passed as the 2nd parameter means the view is not attached to the view hierarchy, which means referenced to it are basically worthless.

    Presuming you are in an activity there should be no need to manually inflate the layout after setContentView has been called. If you do need to keep the inflate, change to the following:

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

    By passing null you cause the inflate routine to miss some important steps so should be avoided even when you don't want to immediately attach the views (ie adapters). The 3rd (optional) parameter decides whether to immediately add the views to the hierarchy.

    As a minimum

    linearLayoutPopup = new LinearLayout(this);
    

    should be replaced with

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

    otherwise you are creating the object, setting things, then replacing it