Android - TextView not showing

I have a TextView that used to show on the screen, and did what I wanted. However, I streamlined some later code that repositions it with a touch to it's parent FrameLayout. Now, the TextView (named angleView) ceases to show on screen, but when I print out it's coordinates, it still works the same way. Can anyone see what I'm doing wrong?

Here is the code from my onCreate() method:

angleView = new TextView(getApplicationContext());
angleView.setTextColor(Color.RED);
angleView.setText("0");
angleView.setLayoutParams(new FrameLayout.LayoutParams(SCREEN_WIDTH/20, SCREEN_HEIGHT/20));

and the code from the FrameLayout's onTouchListener:

preview.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float y = (centerPoint.getY() - event.getY() + STATUS_BAR_HEIGHT);
            float x = (centerPoint.getX() - event.getX());
            double angle = Math.atan(y/x);
            setAngleFieldData(event, angle, y, x);
            setFingerFollower(event, angle);
            setYMeasure(event, y);
            setXMeasure(event, x);
            setTextPositions(event, angle, y, x);
            Log.i("angleX", String.valueOf(angleView.getX()));
            Log.i("angleY", String.valueOf(angleView.getY()));
            return true;
        }

        private void setAngleFieldData(MotionEvent event, double angle, float y, float x){
            angleView.setText(String.valueOf(angle));
            angleView.setX(event.getX());
            angleView.setY(event.getY());
            if(isRadians) {
                if(x < 0 && y >= 0){
                    angleView.setText(String.valueOf(2 * Math.PI + angle));
                }
            }

        }

(the other methods are irrelevant, it used to be that all the code from the individual methods was in the onTouch.)

And in case you are wondering, yes, there is a:

preview.addView(angleView);

Many thanks!

EDIT: All of the code is inside the onCreate() method, inside an Activity.

Answers


ApplicationContext can't modify UI. Change:

angleView = new TextView(getApplicationContext());

By:

angleView = new TextView(this);

Or if you are inside a Fragment:

angleView = new TextView(getActivity());

Need Your Help

Join and subselects

join db2 subquery

I need to rid my iSeries DB2 query of subselects. In the query below, I need to get ALADD1 and ALADD2 (address 1 and 2) from F0116 matched on both ABAN8 and MAPA8 into one record. ABAN8 is the ship...

setEditing:animated: is missing animation?

iphone objective-c ios uitableview

I encountered a problem while programming iOS: When I tried to make my custom edit button for my tableView, I just could not have it animated. Below is how I initialized tableview:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.