Problem with Challenge:From Button to ImageButton


#1

Hello Forum,
i’m on the third Challenge of the second Chapter now and I changed my two buttons exactly like you described it in the book.
The Problem now is when i try to run the App on my device or the emulator it stopps.
I havent modified my QuizActivity.java yet because i don’t know what i should change.
The Problem is propably in the QuizActivity.java.
Could anyone be so kind and tell or explain to me where the error is?
I think it is in this line [mNextButton = (Button)findViewById(R.id.next_button);]
Shouldn’t i change it something like this ? [mNextButton = (ImageButton)findViewById(R.id.next_button);]
I tried it and it doesn’t work :confused:

Here is the code of my QuizActivity.java.

package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
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 Button mTrueButton;
	private Button mFalseButton;
	private Button mNextButton;
	private Button 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_africa, false),
			new TrueFalse(R.string.question_americas, false),
			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);
        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();
			}
		});
        
        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();
			}
		});
        
        mPrevButton = (Button)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) {
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }
    
}

#2

Is the exception you get when you make that change the same exception? It may be that you’ve got multiple issues in your program, and you’ve managed to take a step forward and solve one of them. Reading and understanding what your errors are is a critical skill to develop as an app developer.


#3

I dont understand about what exception you are talking about.
but i will post my two xml files, the one that worked and the one that doesnt.
The QuizActivity.java file is the same for both.

This is the part of activity_quiz.xml with the normal buttons.
With this code it works!

</LinearLayout>
<LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
        <Button 
 	      	android:id="@+id/prev_button"
    	        android:layout_width="wrap_content"
       		android:layout_height="wrap_content"
       		android:text="@string/prev_button"
       		android:drawableLeft="@drawable/arrow_left"
       		android:drawablePadding="4dp"
     		/>
        
        <Button 
       		android:id="@+id/next_button"
    	        android:layout_width="wrap_content"
       		android:layout_height="wrap_content"
       		android:text="@string/next_button"
       		android:drawableRight="@drawable/arrow_right"
       		android:drawablePadding="4dp"
   		    />
            
    </LinearLayout>

Now i change the Code above to the one below and now the app crashes/stops

    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        
        <ImageButton 
 	      	android:id="@+id/prev_button"
    	    android:layout_width="wrap_content"
       		android:layout_height="wrap_content"
       		android:src="@drawable/arrow_left"
       		android:contentDescription="@string/prev_button"
     		/>
        
        <ImageButton 
       		android:id="@+id/next_button"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
   	    	android:src="@drawable/arrow_right"
   	    	android:contentDescription="@string/next_button"
   		    />
            
    </LinearLayout>

I have no errors or warnings in my code.


#4

I just finished the 3rd challenge of Chapter 2 myself.

You cannot use the same QuizActivity.java file for the two xml files.

If you use the second xml file which has the layout for the image buttons, you must change QuizActivity.java


#5

Thank you for your reply atexit8 !
What have you changed in your code?
Do i have to change the onClickListeners?


#6

Now when i looked for the third time over my code i found the error.
I had to change
private Button mNextButton;
to
private ImageButton mNextButton;
and
mNextButton = (Button)findViewById(R.id.next_button);
to
mNextButton = (ImageButton)findViewById(R.id.next_button);

Of course for both buttons.


#7

Hi everyone, I finally got all 3 challenges to work. Wanted to offer some tips, as these can trip you up. When you make a copy of your project, and call it, say, Copy_GeoQuiz (or whatever), MAKE SURE that you close your original project (right click on GeoQuiz folder in package explore, choose close project) AND also close each file window you have open in your main view. Otherwise you could have 2 files that will be named the same in your view, and you might change one file and it’s the wrong one. You should only ever have one project open and being viewed at once.

Another tip, the Emulator is very finicky, so sometimes you have to close it completely and re-run your project (AFTER you save it), to get the most current changes to show up in your emulator.

Yet another tip: when all else fails, sometimes saving or making sure your imports are all there is wise. Keyboard shortcut for updating imports: [control]+[shift]+[O] (that is a letter O, not a zero). I can’t tell you how many times I saw red errors and looked over each file meticulously, only to find nothing to fix and that simply saving my project got rid of my red errors! :stuck_out_tongue: