0

Hi I have tried to run my program however I am getting an error in one of my Java file, however I don't understand the error as the code works perfectly in eclipse, however it bring an error in Android Studio, please advise or help?,

This is the error which I am currently getting, please advise?.

Error:(71, 14) error: no suitable method found for setText(Object)
method TextView.setText(int,BufferType) is not applicable
(actual and formal argument lists differ in length)
method TextView.setText(int) is not applicable
(actual argument Object cannot be converted to int by method invocation conversion)
method TextView.setText(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
method TextView.setText(CharSequence,BufferType) is not applicable
(actual and formal argument lists differ in length)
method TextView.setText(CharSequence) is not applicable
(actual argument Object cannot be converted to CharSequence by method invocation conversion)

Please view the code below, where I am getting the error?

 final TextView login = (TextView) findViewById(R.id.textwelcome);
        login.setText("Welcome  "+user.get("fname"));
final TextView lname = (TextView) findViewById(R.id.lname);
        lname.setText(user.get("lname"));


        }}

Please view the full Java class code?

    public class Main extends Activity {
ImageButton btnLogout;
ImageButton changepas;

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

        changepas = (ImageButton) findViewById(R.id.btchangepass);
        btnLogout = (ImageButton) findViewById(R.id.logout);
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        /**
         * Hashmap to load data from the Sqlite database
         **/
        HashMap user = new HashMap();
        user = db.getUserDetails();

        /**
         * Change Password Activity Started
         **/
        changepas.setOnClickListener(new View.OnClickListener(){
            public void onClick(View arg0){

                Intent chgpass = new Intent(getApplicationContext(), ChangePassword.class);

                startActivity(chgpass);
            }

        });

        /**
         *Logout from the User Panel which clears the data in Sqlite database
         **/

        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                UserFunctions logout = new UserFunctions();
                logout.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), Login.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                finish();
       }
        });
    /**
     * Sets user first name and last name in text view.
     **/
    final TextView login = (TextView) findViewById(R.id.textwelcome);
            login.setText("Welcome  "+user.get("fname"));
    final TextView lname = (TextView) findViewById(R.id.lname);
            lname.setText(user.get("lname"));
     }}
2
  • BTW: class cannot be run/executed. You can only execute methods, often of instance of the class Commented Jun 24, 2015 at 11:25
  • @sam show get("") method Commented Jun 24, 2015 at 11:28

3 Answers 3

1
lname.setText(user.get("lname"));

It seems that user.get("lname") doesn't return a String, yet setText accepts String as parameter.

Try to converse it to a String, or call the toString method on the result.

Sign up to request clarification or add additional context in comments.

2 Comments

(user.get("lname")).toString(); or "" + user.get("lname"); which is like how you did it in the other case: "Welcome "+user.get("fname")
still getting more errors with this, thank u 4 the suggestion,
0

try

lname.setText(""+user.get("lname"));

this will sort your problem.

9 Comments

When I have added this code in, now it cant recognise the R package.
what are you trying to say
all of my other classes it can recognise the R package or class, however it this java file main.java it cannot resolve symbol r .
clean your project and see there might be some error inside one of your xml files which might be blocking generation of R
I will try to clean my project now.
|
0

The error occured because user.get("lname") does not explictly return a String.

You should call toString() or better if your sure that this is a String declare it when you declare your hashmap HashMap<String, String> user = new HashMap<String, String>();

1 Comment

What is inside your hashmap. Obviously the keys are String. But what is the class of the values ? If all values are String you should change the declaration of your HashMap as i suggest in my answer. If not, are you sure the object you get use setText(user.get("lname").toString())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.