pvd
pvd

Reputation: 1343

TextWatcher&Editable cannot be resolved to a type--from "Learning Android"

I am reading the book "Learning Android", I typed the code on page 71, the eclipse show the error:

Editable cannot be resolved to a type...

TextWatcher cannot be resolved to a type...

The method addTextChangedListener(TextWatcher) in the type TextView is not applicable for the arguments ...

I have checked the code between my eclipse and the book for several times, but I can't find the difference between them.

I am a newbie of android, any help will be appreciated.

My code is:

package com.marakana.yamba;

import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class StatusActivity extends Activity implements OnClickListener, TextWatcher{
    private static final String TAG = "StatusActivity";
    EditText editText;
    Button updateButton;
    Twitter twitter;
    TextView textCount;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status);

        //Find views
        editText = (EditText) findViewById(R.id.editText1);
        updateButton = (Button) findViewById(R.id.buttonUpdateStatus);
        updateButton.setOnClickListener(this);

        textCount = (TextView) findViewById(R.id.textCount);
        textCount.setText(Integer.toString(140));
        textCount.setTextColor(Color.GREEN);
        editText.addTextChangedListener(this);

        twitter = new Twitter("hookits", "tw**123");
        twitter.setAPIRootUrl("http://yamba.marakana.com/api");
    }

    // Asynchronously posts to twitter
    class PostToTwitter extends AsyncTask<String, Integer, String> {
        // Called to initiate the background activity
        @Override
        protected String doInBackground(String... statuses){
            try{
                winterwell.jtwitter.Status status = twitter.updateStatus(statuses[0]);
                return status.text;
            } catch (TwitterException e){
                Log.e(TAG, e.toString());
                e.printStackTrace();
                return "Failed to post";
            }
        }
        // Called when there's a status to be updated
        @Override
        protected void onProgressUpdate(Integer... values){
            super.onProgressUpdate(values);
        }

        // Called once the background activity has completed
        @Override
        protected void onPostExecute(String result){
            Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }

    //TextWatcher methods
    public void afterTextChanged(Editable statusText){
        int count = 140 - statusText.length();
        textCount.setText(Integer.toString(count));
        textCount.setTextColor(Color.GREEN);
        if (count < 10)
            textCount.setTextColor(Color.YELLOW);
        if (count < 0)
            textCount.setTextColor(Color.RED);
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
    // Called when button is clicked//
    public void onClick(View v){
        String status = editText.getText().toString();
        new PostToTwitter().execute(status);
        //twitter.setStatus(editText.getText().toString());
        Log.d(TAG, "onClicked");
    }
}

Upvotes: 0

Views: 3564

Answers (1)

kaspermoerch
kaspermoerch

Reputation: 16570

You are missing this import:

import android.text.TextWatcher;

Add it, and it should work.

Note: After you add the import Eclipse might tell you that you have "unimplemented methods". Just add them and move your code around to fit it.

Edit

Also add:

import android.text.Editable;

Upvotes: 6

Related Questions