Android 为什么setText()在TextView上的以下代码中不起作用?
活动布局如下:Android 为什么setText()在TextView上的以下代码中不起作用?,android,android-layout,textview,settext,Android,Android Layout,Textview,Settext,活动布局如下: // All required Imports public class PuzzleActivity extends Activity implements OnClickListener{ private PuzzlerDB db; private Puzzle puz; private int puzId=1; private String puzAns=""; private int score=0;
// All required Imports
public class PuzzleActivity extends Activity implements OnClickListener{
private PuzzlerDB db;
private Puzzle puz;
private int puzId=1;
private String puzAns="";
private int score=0;
private TextView tvPuzContent;
private EditText etPuzAns;
private Button btnCheck;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_puzzle);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
db = new PuzzlerDB(this);
puz = db.getPuzzle(puzId);
puzAns = puz.getAnswer();
tvPuzContent = (TextView) findViewById(R.id.tv_puz_content);
tvPuzContent.setText(puz.getContent());
btnCheck = (Button) findViewById(R.id.btn_check);
btnCheck.setOnClickListener(this);
android.util.Log.v("IN SIDE ACTIVITY: ", "id: "+ puzId + " answer: "+puz.getAnswer());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_check:
checkAndNotify(puzAns);
break;
}
}
private void checkAndNotify(String puzAns)
{
String ans = ((EditText)findViewById(R.id.te_puz_ans)).getText().toString();
if(ans.trim().equalsIgnoreCase(puzAns.trim()))
{
//show pop-up dialog for right answer
new AlertDialog.Builder(this).setTitle("The Puzzler:")
.setMessage("Wow! Right Answer!!")
.setNeutralButton("Continue", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int sumthin)
{
android.util.Log.v("IN SIDE function:", " onClick of Dialog");
update();
}
}).show();
}else{
new AlertDialog.Builder(this).setTitle("The Puzzler:")
.setMessage("Sorry! You have to rethink!!")
.setNeutralButton("Continue", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dlg, int sumthin)
{
// do nothing – it will close on its own
}
}).show();
}
}
protected void update() {
score = score + 10;
++puzId;
}
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_puzzle);
puz = db.getPuzzle(puzId);
if(puz != null)
{
tvPuzContent.setText(puz.getContent());
android.util.Log.v("IN SIDE onResume:", " Content: "+ puz.getContent());
puzAns = puz.getAnswer();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
}
db查询正确地返回了我可以通过logcat检查的内容。但是文本视图没有显示。编辑文本和按钮正在显示
我尝试过其他帖子中的各种选项,比如(I)使TextView成为静态(ii)以字符串形式接收拼图内容,然后通过setText()和其他方法设置,但都没有成功。请您提出解决方案。您正在调用
setContentView(R.layout.activity\u puzzle)代码>在onResume()
中,但是tvPuzContent
变量在onCreate()
中实例化,因此它引用了onCreate
中对setContentView
的原始调用中的文本视图
基本上,在onCreate
结束时,您已经分配了所有变量,但随后系统会调用onResume
,您再次调用setContentView
,这会丢弃您的原始布局,因此您的所有变量都指向不在屏幕上的旧视图
不要在onResume()
中调用setContentView
,因为您不会更改布局。您在puz var中得到什么了吗?您在这个日志中得到了什么?v(“在简历中:”,“内容:”+puz.getContent());对puz正在设置其所有数据成员的值。我在上面的日志中得到了谜题的内容,比如:'01-23 22:28:17.937:V/in-SIDE-on-resume:(18673):content:2 x 5的值是多少?'is puz.getContent()类型字符串?也许您调用了TextView setText()的另一个重载,然后调用了setText(CharSequence text)…谢谢您的回答。我纠正了那部分。我从onResume()中删除了setContentView(),并将内容显示在屏幕上。但是,为什么在关闭警报对话框后不调用onResume()?如果我没有记错的话,因为在对话框结束后活动已进入前台,应该调用onResume()。这应该通过getPuzzle()获得新的谜题,并且应该更新textview?如何用新的益智内容重新绘制活动?我不认为对话框算作将您的活动放到背景中——对话框本质上是活动中的另一个视图。只有其他活动才能使您的活动进入后台。如果新活动是透明的或不是全屏的,它将只触发onPause/onResume,而不触发onStop/onRestart/onStart。如果您希望在对话框关闭时发生某些事情,请为其创建一个方法,并从对话框的按钮侦听器中调用该方法。这就是重点。我的印象是,当任何东西阻碍一项活动时,它就是“暂停”。但当阻塞元素(对话框就是这种情况)是活动本身的另一个组件时,情况并非如此+这是我的荣幸。谢谢
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgimg"
android:layout_gravity="center">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="20dp"
android:stretchColumns="*">
<TableRow>
<TextView android:id="@+id/tv_puz_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="3"
android:typeface="serif"
android:textStyle="bold"
android:textSize="20sp"
android:text="" />
</TableRow>
<TableRow>
<EditText android:id="@+id/te_puz_ans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_span="3"
android:hint="Answer" />
</TableRow>
<TableRow android:gravity="center">
<Button android:id="@+id/btn_check"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:hint="Check" />
<Button android:id="@+id/btn_skip"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:hint="Skip" />
<Button android:id="@+id/btn_hint"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:hint="Hint" />
</TableRow>
</TableLayout>
</LinearLayout>