ComponentInfo Activity?


#1

Hello - Geoquiz’s title bar shows up in the emulator, but no buttons or text show up, it just crashes. The first error message says “Unable to start activity ComponentInfo” but I didn’t create an activity with that name, and I can’t find that activity in the gen folder either. Help.


#2

You didn’t give us much to go on but if you have a real Android device handy, does it work on that?


#3

I’m trying but I can’t get it to show up in Eclipse, I think because the USB connector is bad because all the settings seem right. I’m due for an upgrade in a few weeks so perhaps I’ll have to wait on an answer till then.

Sorry, I wasn’t trying to be incomplete, I just don’t know what would be relevant to post!


#4

Scratch that, I just tried it on my Android 2.2.3 device & had the same issue.

This is the code as of Chapter 2 but I was getting the same error at the end of Chapter 1.
Quiz.java

[code]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 Quiz extends Activity {

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) {
	//get user's answer 
	boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
	int messageResId = 0;
	//compare user's answer to truefalse object 
	if (userPressedTrue == answerIsTrue) {
			//make a toast to display appropriate message
			messageResId = R.string.correct_toast;
			}
			else { 
			messageResId = R.string.incorrect_toast;
			}
}
	
			@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	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.string.next_button);
	mNextButton.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View arg0) {
			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;
}

}[/code]
activity_quiz.xml

[code]

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"             />


<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

	<Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button" />

	<Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />
[/code] AndroidManifest.xml[code] <?xml version="1.0" encoding="utf-8"?>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.bignerdranch.android.geoquiz.Quiz"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

[/code]


#5

Sorry, I don’t see anything obvious. You didn’t post your contents of quiz.xml so I didn’t look at that.

As a test, you might try commenting out all but the first two lines of onCreate() to see if the UI displays correctly without crashing.


#6

Thanks this makes me feel better… I just tried your suggestion of commenting stuff out and it still crashes the same way. What’s Quiz.xml? I have activity_quiz.xml but no Quiz.xml.

(edited)
Here it is… I haven’t even gotten to that chapter yet.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

</menu>

#7

Look at your code for onCreateOptionsMenu(). That’s why you need quiz.xml. In the book source code it’s called main.xml. I’ve noticed you’re using slightly different names than the book but of course that shouldn’t matter.

At this point I guess I’ll just give my standard advice. Download the source from the book and see if it works. If it does, it’s just a matter of finding the difference.


#8

Update, as a wise friend of mine told me, ComponentInfo was a red herring & I needed to keep looking thru the stack trace to find the real problem, which was that I had the wrong thing for the Next button (R.string instead of R.id). Thank you!