Java 我想设置code.tostring,但它不是';不可用
在注释标记的代码行中,我想将TownhallVL放在其中,但它必须是字符串,所以我尝试使用.toString,但它不可用。 有没有别的解决办法,或者我做错了什么Java 我想设置code.tostring,但它不是';不可用,java,android,Java,Android,在注释标记的代码行中,我想将TownhallVL放在其中,但它必须是字符串,所以我尝试使用.toString,但它不可用。 有没有别的解决办法,或者我做错了什么 package com.example.android.planmyclash; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainAc
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
//declare ids
TextView townHallText = (TextView) findViewById(R.id.town_hall_lvl);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setValues();
}
public void setValues(){
townHallText.setText(); //this line <----
}
}
package com.example.android.planmyclash;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.TextView;
公共类MainActivity扩展了AppCompatActivity{
//设定建筑物的价值
int townHallLvl=1;
//声明ID
TextView townHallText=(TextView)findViewById(R.id.town\u hall\u lvl);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setValues();
}
公共void setValues(){
townHallText.setText();//此行使用
要将整数转换为字符串,请尝试以下操作:
townHallText.setText(townHallLvl+"");
我希望它能完美工作。package com.example.android.planmyclash;
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
TextView townHallText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare ids
townHallText = (TextView) findViewById(R.id.town_hall_lvl);
setValues();
}
public void setValues(){
townHallText.setText(townHallLvl + ""); //this line <----
}
}
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.TextView;
公共类MainActivity扩展了AppCompatActivity{
//设定建筑物的价值
int townHallLvl=1;
TextView Townhall文本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//声明ID
townHallText=(TextView)findViewById(R.id.town\u hall\u lvl);
setValues();
}
公共void setValues(){
townHallText.setText(townhallllvl+);//这一行“它不可用”是什么意思?您到底尝试了什么,发生了什么?您试图调用toString
on什么?使用townHallText.setText(String.valueOf(townhallllvl));
将整数转换为字符串。setText()
需要一个字符串
或字符串资源ID作为参数。谢谢,伙计。我想你应该把它作为答案,而不是comment@MohammedAoufZOUAG在答案部分使用它,用户会很快看到它是的,他是对的!!!
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
TextView townHallText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare ids
townHallText = (TextView) findViewById(R.id.town_hall_lvl);
setValues();
}
public void setValues(){
townHallText.setText(townHallLvl + ""); //this line <----
}
}