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

The activity-layout is as follows:

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

The db query is returning content properly which I can check through logcat. But the text view is not showing up.The EditText and buttons are being displayed though.

I have tried various options from other posts like (i )making TextView static (ii) receiving the puzzle Content in a string and then setting it through setText() and other things with no success. Could you pls, suggest a solution to this.

Dexter
  • 4,036
  • 3
  • 47
  • 55
  • did you get something in puz var? – Saqib Jan 23 '14 at 17:27
  • what are you getting in this log? android.util.Log.v("IN SIDE onResume:", " Content: "+ puz.getContent()); – pozuelog Jan 23 '14 at 18:01
  • Yes. puz is having all its data members' value set. And I am getting the content of the puzzle in above log like: '01-23 22:28:17.937: V/IN SIDE onResume:(18673): Content: What is the value of 2 x 5?' – Dexter Jan 23 '14 at 18:12
  • Is puz.getContent() type string? Maybe you call another overloading of TextView setText() then setText(CharSequence text)... – gipinani Jan 23 '14 at 18:23

1 Answers1

8

You are calling setContentView(R.layout.activity_puzzle); in onResume(), but the tvPuzContent variable is instantiated in onCreate(), so it refers to the text view from the original call to setContentView in onCreate.

Basically, by the end of onCreate you have assigned all your variables, but then onResume is called by the system and you call setContentView again, which throws away your original layout, so all your variables are pointing at old views that are not on screen.

Just don't call setContentView in onResume(), since you aren't changing the layout anyway.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Thanks for answering. I corrected that part. I removed setContentView() from onResume() and got the content on the screen. But why is onResume() not being called after dismissing the alert dialog? If I am not wrong, as the activity has come to foreground after dialog dismissal onResume() should be called. This should have get the new puzzle through getPuzzle() and should have updated the textview? How can I redraw the activity with new puzzle content? – Dexter Jan 23 '14 at 19:37
  • 2
    I don't think a dialog counts as putting your activity into the background--the dialog is essentially another View within your Activity. Only another activity can make your activity go into the background. If the new activity is transparent or not full-screen, it will only trigger onPause/onResume without triggering onStop/onRestart/onStart. If you want something to happen when the dialog closes, create a method for it, and call that method from the button listeners of the dialog. – Tenfour04 Jan 24 '14 at 02:05
  • That's the point. I was of the impression that when anything blocks an activity, it is "onPaused". But this is not the case when the blocking element (Dialog is this case) is another component of the Activity itself. +1 for this. Thanks. – Dexter Jan 25 '14 at 11:37