Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t获取文本视图以显示txt文件中的文本_Java_Android - Fatal编程技术网

Java Can';t获取文本视图以显示txt文件中的文本

Java Can';t获取文本视图以显示txt文件中的文本,java,android,Java,Android,我是一个新手程序员,可能已经超越了我自己,但是到目前为止我所写的代码似乎不起作用,我已经花了几个小时试图弄清楚它 问题是,当我运行这个应用程序时,TextView仍然显示为空 public class MainActivity extends Activity { // GUI controls TextView thoughtLogView; /** Called when the activity is first created. */ @Override public void onC

我是一个新手程序员,可能已经超越了我自己,但是到目前为止我所写的代码似乎不起作用,我已经花了几个小时试图弄清楚它

问题是,当我运行这个应用程序时,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>


您不在调用
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();
}

只有在执行完完整的方法之后,才能从方法
GetPhoneAddress()
获得字符串结果

问题是,当我运行这个应用程序时,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;
}
这是因为您在
saved\u phone
中没有值,并且您也没有调用
GetPhoneAddress()
,因此我建议您将这些代码行移到方法
GetPhoneAddress()
之外,正确调用它并检查字符串,然后将其设置为
TextView

String saved_phone = GetPhoneAddress();
    if (saved_phone.length()>0){
        tvphone.setText(saved_phone);
    }
把这条线移到

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

}

问题是,在返回语句之前,您当前正试图从方法本身GetPhoneAddress()获取返回字符串,所以它将不返回任何内容,您需要在调用GetPhoneAddress()之后设置文本,并且可以如上所述实现这一点。

您不调用
GetPhoneAddress()
method.call GetPhoneAddress()方法在onCreate中创建