Reputation: 31
I have no idea whats causing my app to crash, the logcat says its getting a fatal exception from MAIN, i don't really know what that means im pretty new to this stuff. What happens is the first activity loads fine but as soon as i press the button to load the second activity is stops. Sorry if its a too long question or already been asked, i've tried a lot of things posted in previously asked questions.
Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".QuizActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:exported="false"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT">
</category>
</intent-filter>
</activity>
<activity
android:name=".MainMenu"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
first activity
public class MainMenu extends Activity {
Button quizButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mainmenu);
quizButton = (Button)findViewById(R.id.launchQuiz);
quizButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == quizButton.getId()) {
Intent launchQuizActivity = new Intent(MainMenu.this, QuizActivity.class);
MainMenu.this.startActivity(launchQuizActivity);
}
}
});
}
}
and heres the second one:
public class QuizActivity extends Activity {
Intent returnHome = new Intent(QuizActivity.this, matt.apps.quiz.MainMenu.class);
String questions[] = {"What month is it?","What OS is this?" };
String lChoices[] = {"July","IOS"};
String cChoices[] = {"August","Android"};
String rChoices[] = {"October", "Windows?"};
int answers[] = {R.id.lButton,R.id.cButton };
boolean correct = false;
int numCorrect = 0;
int numWrong = 0;
int btnClicked;
int i = -1;
int t = 0;
Button lButton;
Button cButton;
Button rButton;
Button nextButton;
TextView questionTV;
TextView greetingTV;
public boolean getCorrect(int id) {
if (id == answers[i]) {
correct = true;
} else {
correct = false;
}
return correct;
}
OnClickListener clicker = new OnClickListener() {
//TODO change to method to handle answers
@Override
public void onClick(View v) {
btnClicked = v.getId();
if (btnClicked == R.id.nextButton) { // next button
i += 1;
if (i <= questions.length) {
questionTV.setText(questions[i]);
lButton.setText(lChoices[i]);
cButton.setText(cChoices[i]);
rButton.setText(rChoices[i]);
nextButton.setText("Next question");
} else {
questionTV.setText("You got " + numCorrect + " right and " + numWrong + " wrong.");
lButton.setText("");
cButton.setText("");
rButton.setText("");
} // end of next button
} else { // answer buttons
if (i > -1) {
if (i <= questions.length) {
if (getCorrect(btnClicked) == true) {
numCorrect += 1;
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
} else {
numWrong += 1;
Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
}
}
}
}// end of answer buttons
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
greetingTV = (TextView)findViewById(R.id.greetingTV);
questionTV = (TextView)findViewById(R.id.questionTV);
lButton = (Button)findViewById(R.id.lButton);
cButton = (Button)findViewById(R.id.cButton);
rButton = (Button)findViewById(R.id.rButton);
nextButton = (Button)findViewById(R.id.nextButton);
//set on click listeners for buttons
questionTV.setText("Please click the start button to begin.");
nextButton.setText("Start");
}
}
Error Log:
E/AndroidRuntime( 620): FATAL EXCEPTION: main
E/AndroidRuntime( 620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{matt.apps.quiz/matt.apps.quiz.QuizActivity}: java.lang.NullPointerException
E/AndroidRuntime( 620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
E/AndroidRuntime( 620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime( 620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime( 620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
Upvotes: 0
Views: 204
Reputation: 8304
You have a NullPointer (it says so at the end of the first line after FATAL EXCEPTION in your logcat). Just trace to where the stacktrace mentions your package name (right now it's only showing up to android.app.ActivityThread) - just like reading a Java stack trace.
Upvotes: 1
Reputation: 10472
Well Matthagan, the LogCat has the fatal exception. Its in Red. Now what you want to do is first of all always print at least the first 4 lines after fatal so people know what you got. If you want to see the exact spot and your in eclipse, goto the first line below fatal that contains YOUR PACKAGE NAME. Click on this line an it will take you to the exact spot.
Here is what it could be.
1) quizButton = (Button)findViewById(R.id.launchQuiz); returns null so you get a null pointer exception. This could occur becase launchQuiz is is not properly definied the your layout.
2) You might not have a height and width specified for an element in the layout.
Upvotes: 1