Reputation:
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"));
}}
Upvotes: 0
Views: 1490
Reputation: 1548
try
lname.setText(""+user.get("lname"));
this will sort your problem.
Upvotes: 0
Reputation: 1904
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>();
Upvotes: 0
Reputation: 9437
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.
Upvotes: 1