0

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.

ro Ro
  • 123
  • 10
  • you should declare view. viewgroup.findViewById(R.id.textviewID); – Elias Fazel Feb 28 '17 at 03:04
  • oops sorry! I missed the code by mistake. Included in the original source. It's corrected.. – ro Ro Feb 28 '17 at 03:11
  • Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working will bump it to the top of the active queue. – Mike M. Feb 28 '17 at 03:46
  • Sorry I did not know about it. I'll be careful ahead of time. – ro Ro Feb 28 '17 at 04:39

1 Answers1

1

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

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124