Logging the Activity Lifecycle


#1

I am working in Chapter 3, Logging the Activity Lifecycle and I am working from Listing 3.3 Overriding more lifecycle methods (QuizActivity.java)

I have typed exactly what is in the book (or, at least, I think I have) but I keep getting errors. So far, I just have the first onStart coded.

This is what I have


package com.bignerdranch.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {
	private static final String TAG = "QuizActivity";
	

	private Button mTrueButton;
	private Button mFalseButton;
	private ImageButton mNextButton;
	private ImageButton mPrevButton;
	private TextView mQuestionTextView;
	
	private TrueFalse[] mQuestionBank = new TrueFalse[] {
			new TrueFalse(R.string.question_oceans, true),
			new TrueFalse(R.string.question_mideast, false),
			new TrueFalse(R.string.question_americas, true),
			new TrueFalse(R.string.question_asia, true),
	};
	
	private int mCurrentIndex = 0;	
	
	private void updateQuestion() 
	{
		int question = mQuestionBank[mCurrentIndex].getQuestion();
		mQuestionTextView.setText(question);
	}
	
	private void checkAnswer(boolean userPressedTrue){
		boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
		
		int messageResId = 0;
		
		if (userPressedTrue == answerIsTrue) {
			messageResId = R.string.correct_toast;
		}else{
			messageResId = R.string.incorrect_toast;
		}
		Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
			
		
	}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);
        
        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
				updateQuestion();
				
			}
			
			@Override
			public void onStart() {
				super.onStart();
				Log.d(TAG, "onStart() called");
				
			}
		});
        
        
        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				checkAnswer(true);
				
			}
		});
        
        mFalseButton = (Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				checkAnswer(false);
				
				
				
			}
		});
        
        mNextButton = (ImageButton)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
				updateQuestion();
				
			}
		});
        
        updateQuestion();
        
        mPrevButton = (ImageButton)findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mCurrentIndex = (mCurrentIndex -1) % mQuestionBank.length;
				updateQuestion();
				
			}
		});
        
        updateQuestion();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }
    
}

my errors are in this block of code:

@Override
			public void onStart() {
				super.onStart();
				Log.d(TAG, "onStart() called");

The first error says:
The method onStart() of type new View.OnClickListener(){} must override or implement a supertype method

Second error:
The method onStart() is undefined for the type Object

Please, what can I do to get this straightened out???


#2

Your onStart method is inside of onCreate method. You have to put it outside of onCreate method.


#3

Hello! I’m also on Chapter 3, and am up to page 59. I am using an actual device for my apps. When I run the app, using the QuizActivity filter as prescribed, nothing shows up in the log. Here is my code from QuizActivity.java:

[code]package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

private static final String TAG = "QuizActivity";

private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;

private TrueFalse[] mQuestionBank = new TrueFalse[] {
		new TrueFalse(R.string.question_oceans, true),
		new TrueFalse(R.string.question_mideast, false),
		new TrueFalse(R.string.question_africa, false),
		new TrueFalse(R.string.question_americas, true),
		new TrueFalse(R.string.question_asia, true),
};

private int mCurrentIndex = 0;

private void updateQuestion() {
	int question = mQuestionBank[mCurrentIndex].getQuestion();
	mQuestionTextView.setText(question);
}


private void checkAnswer(boolean userPressedTrue){
	boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
	
	int messageResId = 0;
	
	if (userPressedTrue == answerIsTrue) {
		messageResId = R.string.correct_toast;
	} else {
		messageResId = R.string.incorrect_toast;
	}
	
	Toast.makeText(this,messageResId, Toast.LENGTH_SHORT).show();
	
}

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	Log.d(TAG, "onCreate(Bundle) called");
	setContentView(R.layout.activity_quiz);
	
	mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
	
	mTrueButton = (Button)findViewById(R.id.true_button);
	mTrueButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v){
			checkAnswer(true);
		}
	});
	
	mFalseButton = (Button)findViewById(R.id.false_button);
	mFalseButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			checkAnswer(false);
		}
	});
	
	mNextButton = (Button)findViewById(R.id.next_button);
	mNextButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v){
			mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
			updateQuestion();
		}
	});
	
	updateQuestion();
}

@Override
public void onStart() {
	super.onStart();
	Log.d(TAG, "onStart() called");
}

@Override
public void onPause() {
	super.onPause();
	Log.d(TAG, "onResume() called");
}

@Override
public void onResume() {
	super.onResume();
	Log.d(TAG, "onResume() called");
}

@Override
public void onStop() {
	super.onStop();
	Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
	super.onDestroy();
	Log.d(TAG, "onDestroy() called");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.quiz, menu);
	return true;
}

}[/code]

Anything that needs fixed? Thanks for your help. Peace and God Bless.


#4

What happens when you use an emulator?


#5

All right, yes it works. I wonder why it doesn’t work using a real device?


#6

I had/have same problem. But when I opened LogCat (depreciated) I found logs there.


#7

[quote=“DCrawshawJr”]Hello! I’m also on Chapter 3, and am up to page 59. I am using an actual device for my apps. When I run the app, using the QuizActivity filter as prescribed, nothing shows up in the log. Here is my code from QuizActivity.java:

[code]package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

private static final String TAG = "QuizActivity";

private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;

private TrueFalse[] mQuestionBank = new TrueFalse[] {
		new TrueFalse(R.string.question_oceans, true),
		new TrueFalse(R.string.question_mideast, false),
		new TrueFalse(R.string.question_africa, false),
		new TrueFalse(R.string.question_americas, true),
		new TrueFalse(R.string.question_asia, true),
};

private int mCurrentIndex = 0;

private void updateQuestion() {
	int question = mQuestionBank[mCurrentIndex].getQuestion();
	mQuestionTextView.setText(question);
}


private void checkAnswer(boolean userPressedTrue){
	boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
	
	int messageResId = 0;
	
	if (userPressedTrue == answerIsTrue) {
		messageResId = R.string.correct_toast;
	} else {
		messageResId = R.string.incorrect_toast;
	}
	
	Toast.makeText(this,messageResId, Toast.LENGTH_SHORT).show();
	
}

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	Log.d(TAG, "onCreate(Bundle) called");
	setContentView(R.layout.activity_quiz);
	
	mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
	
	mTrueButton = (Button)findViewById(R.id.true_button);
	mTrueButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v){
			checkAnswer(true);
		}
	});
	
	mFalseButton = (Button)findViewById(R.id.false_button);
	mFalseButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			checkAnswer(false);
		}
	});
	
	mNextButton = (Button)findViewById(R.id.next_button);
	mNextButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v){
			mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
			updateQuestion();
		}
	});
	
	updateQuestion();
}

@Override
public void onStart() {
	super.onStart();
	Log.d(TAG, "onStart() called");
}

@Override
public void onPause() {
	super.onPause();
	Log.d(TAG, "onResume() called");
}

@Override
public void onResume() {
	super.onResume();
	Log.d(TAG, "onResume() called");
}

@Override
public void onStop() {
	super.onStop();
	Log.d(TAG, "onStop() called");
}

@Override
public void onDestroy() {
	super.onDestroy();
	Log.d(TAG, "onDestroy() called");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.quiz, menu);
	return true;
}

}[/code]

Anything that needs fixed? Thanks for your help. Peace and God Bless.[/quote]
Hi, I ran into the same issue, and I found that using Log.i(…) instead of Log.d(…) may fix the problem. Hope it can help you.