为什么setText()在TextView上的以下代码中不起作用?

时间:2014-01-23 17:09:16

标签: 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;

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

活动布局如下:

<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>

db查询正在正确返回内容,我可以通过logcat检查。但是文本视图没有显示出来。虽然正在显示EditText和按钮。

我尝试了其他帖子中的各种选项,例如(i)使TextView静态(ii)在字符串中接收拼图内容,然后通过setText()和其他事情设置它没有成功。你能不能提出解决方案。

1 个答案:

答案 0 :(得分:8)

您正在setContentView(R.layout.activity_puzzle);中调用onResume(),但tvPuzContent变量已在onCreate()中实例化,因此它引用了原始调用{{1}的文本视图在setContentView

基本上,在onCreate结束时,您已分配了所有变量,但系统会调用onCreate,然后再次调用onResume,这会丢弃原始布局,所以你的所有变量都指向不在屏幕上的旧视图。

请不要在setContentView中致电setContentView,因为您还没有改变布局。