2016-05-29 55 views
-2

在標記爲註釋的代碼行中,我想放置townHallLvl,但它需要是字符串,因此我嘗試使用.toString但它不可用。 是否有其他解決方案或我犯了錯誤。我想設置代碼.tostring,但它不可用

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 <---- 
} 

} 
+0

你說的 「它不是用」 是什麼意思?你究竟做了什麼,發生了什麼?你打算給'toString'打什麼? –

+0

使用'townHallText.setText(String.valueOf(townHallLvl)); '爲了將整數轉換爲一個字符串。 'setText()'需要一個'String'或一個字符串資源ID作爲參數。 –

+0

謝謝你。我認爲你應該把它作爲答案而不是評論 –

回答

1

使用

townHallText.setText(String.valueOf(townHallLvl)); 

以整數轉換爲字符串。

+0

是的它的作品! –

0

試試這個:

townHallText.setText(townHallLvl+""); 

我希望它會很好地工作。

0
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 <---- 
} 

} 

快樂編碼

相關問題