2016-03-28 10 views
-3

私は初心者のプログラマーであり、自分自身より先に進んでいるかもしれませんが、これまでに書いたコードはうまくいかず、私はそれを理解しようとしています時間のためにテキストファイルからテキストを表示するためのTextViewを取得できません

問題は、私がこのアプリケーションを実行すると、TextViewがまだ空になることです。

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

そしてここでは、レイアウトファイルです。このようにコードを変更してください。

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()の呼び出しと上記のように達成することができます

関連する問題