logtainment
logtainment

Reputation: 9

Handler cannot be converted to View

i have a question! Why is my paramView = new Handler(); in red? My error log is error: incompatible types: Handler cannot be converted to View. How can i fix it?

    public void startTimer(final View paramView)
    {
        paramView = new Handler();
        Timer localTimer = new Timer();
        this.timerTask = new TimerTask()
        {
            public void run()
            {
                paramView.post(new Runnable()
                {
                    public void run()
                    {
                        ((TextView)MainActivity.this.findViewById(R.id.timer)).setText(MainActivity.this.myTimer + "");
                        MainActivity localMyActivity = MainActivity.this;
                        localMyActivity.myTimer += 1;
                    }
                });
            }
        };
        localTimer.schedule(this.timerTask, 0L, 10L);
    }

Please help me to fix the Problem

Upvotes: 0

Views: 69

Answers (2)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

paramView = new Handler();

This is wrong becuase paramView is a View, not a handler. A View cannot be cast to a Handler, nor do they have anything in common so to speak. One is used to show something on the UI, the other on to do something.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

get rid of paramView = new Handler();. Of course you can't assign a Handler to a View. And it this case you don't need a Handler either. View has the post method, that adds the Runnable to the message queue. Here you can find the documentation

Upvotes: 2

Related Questions