2016-03-28 61 views
-3

我是一個新手程序員,可能會領先於我自己,但我迄今爲止編寫的代碼似乎不起作用,我一直試圖弄清楚它幾小時無法讓TextView顯示來自txt文件的文本

問題是,當我運行這個應用程序時,TextView仍然顯示爲空。

public class MainActivity extends Activity 
{ 
// GUI controls 
TextView thoughtLogView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    //Set MainActivity.xml as user interface layout 
    setContentView(R.layout.main); 
    // bind GUI elements with local controls 

    thoughtLogView = (TextView) findViewById(R.id.logTextView); 
} 

private String GetPhoneAddress() { 
    File file = new File(Environment.getExternalStorageDirectory() + "mythoughtlog.txt"); 
    if (!file.exists()){ 
     String line = "Need to add smth"; 
     return line; 
    } 
    String line = null; 
    //Read text from file 
    //StringBuilder text = new StringBuilder(); 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     line = br.readLine(); 
    } 
    catch (IOException e) { 
     //You'll need to add proper error handling here 
    } 

    final TextView tvphone = (TextView) findViewById(R.id.logTextView); 
    String saved_phone = GetPhoneAddress(); 
    if (saved_phone.length()>0){ 
     tvphone.setText(saved_phone); 
    } 
    return line; 
} 

而這裏的佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="top|center" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/title" 
    android:textStyle="normal" 
    android:gravity="center" 
    android:layout_marginTop="10dp" 
    android:textSize="20sp" 
    android:textColor="#FFFFFF"/> 

<TextView 
    android:layout_width="242dp" 
    android:layout_height="227dp" 
    android:id="@+id/logTextView" 
    android:background="#ffffff" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom|center"> 

    <Button 
     android:layout_height="80dp" 
     android:text="New Log" 
     android:layout_width="match_parent" 
     android:layout_weight="1.0" 
     android:textSize="25sp" 
     android:onClick="onNewLogButtonClick"/> 

</LinearLayout> 

+2

你沒有調用'GetPhoneAddress()'方法。 –

+1

在您的onCreate中調用** GetPhoneAddress()**方法 –

回答

1

你不叫GetPhoneAddress()那的原因,你不能設置文本。所以用這種方式改變你的代碼。

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    //Set MainActivity.xml as user interface layout 
    setContentView(R.layout.main); 
    // bind GUI elements with local controls 

    thoughtLogView = (TextView) findViewById(R.id.logTextView); 
    GetPhoneAddress(); 
} 
0

只有在執行完整的方法後,纔會從方法GetPhoneAddress()獲得字符串結果。

問題是,當我運行這個應用程序時,TextView仍然顯示爲空。

這是因爲你在saved_phone沒有價值,你是不是叫GetPhoneAddress()要麼,所以我建議你移動代碼的這些線路的方法GetPhoneAddress()外,正確地調用它,將其設置爲TextView前檢查字符串。

String saved_phone = GetPhoneAddress(); 
    if (saved_phone.length()>0){ 
     tvphone.setText(saved_phone); 
    } 
0

移動這條線到

onCreate(Bundle savedInstanceState){ 
GetPhoneAddress(); 
final TextView tvphone = (TextView) findViewById(R.id.logTextView); 
String saved_phone = GetPhoneAddress(); 
if (saved_phone.length()>0){ 
    tvphone.setText(saved_phone); 
} 

} 

問題是什麼,你正在試圖return語句之前,從方法本身GetPhoneAddress()得到返回的字符串,所以它會返回任何結果,你需要後置文本調用GetPhoneAddress()並且你可以達到上述效果