我是一名新手程序员,可能会超越自己,但到目前为止我编写的代码似乎无法正常工作,而且我一直在努力解决这个问题
问题在于,当我运行此应用程序时,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>
答案 0 :(得分: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();
}
答案 1 :(得分:0)
只有在执行完整方法后,才会从方法GetPhoneAddress()
获取String结果。
问题在于,当我运行此应用程序时,TextView仍然显示为空。
这是因为saved_phone
没有任何价值,而且您也没有调用GetPhoneAddress()
,所以我建议您将这些代码行移到方法GetPhoneAddress()
之外,正确调用它在将字符串设置为TextView
之前检查字符串。
String saved_phone = GetPhoneAddress();
if (saved_phone.length()>0){
tvphone.setText(saved_phone);
}
答案 2 :(得分:0)
将此行移至
var Base = React.createClass({
getInitialState() {
return {current: 1}
},
render() {
return (
<div>{this.state.current}</div>
<div>{this.props.children}</div>
<div>{this.props.demo}</div>
);
}
});
export default connect(state => ({
demo: state.demo
}))(Base);
---
var A = React.createClass({
componentDidMount() {
// how to set base class state
//
this.props.setDemo('demo');
},
render() {
....
}
});
export default connect(() => ({}), {
setDemo
});
问题是你目前正在尝试从return方法本身返回字符串GetPhoneAddress()之前返回语句所以它什么都不返回,你需要在调用GetPhoneAddress()之后设置文本,你可以实现上面的