When I run this code the app crashes and I get an "Unable to start activity ComponentInfo" error, and I noticed that this happens when I initialize the cursor. Here is the method thar returns a Cursor in my DBadapter class:
public Cursor getAllQuestionsParNiveau(String niveau){
String sql = "SELECT designQuestion FROM Question, Quiz, Question_Quiz " +
"WHERE Question.idQuestion = Question_Quiz.idQuestion " +
"AND Quiz.idQuiz = Question_Quiz.idQuiz " +
"AND Quiz.niveauQuiz = '" + niveau + "'";
return db.rawQuery(sql, null);
}
This is the activity where I call the method:
package com.example.quiz;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Quizz extends Activity {
private ArrayList<String> listQuestions = new ArrayList<String>();
private DBAdapter adapter;
private Cursor cursor;
private Button suivant;
private Button precedent;
private TextView question;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizz);
suivant = (Button)findViewById(R.id.suivant);
precedent = (Button)findViewById(R.id.precedent);
question = (TextView)findViewById(R.id.question);
try{
cursor = adapter.getAllQuestionsParNiveau("Débutant");
while(cursor.moveToNext()){
listQuestions.add(cursor.getString(0));
}
}
catch(SQLException se){
Toast.makeText(this, "Erreur !\n" + se.getMessage(), Toast.LENGTH_LONG).show();
}
//question.setText(listQuestions.get(0));
question.setText("hahahaha");
/*suivant.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
i = i+1;
question.setText(listQuestions.get(i));
}
});
precedent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
i = i-1;
question.setText(listQuestions.get(i));
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.quizz, menu);
return true;
}
}
And this is the log:
04-07 15:32:06.973: E/Trace(2001): error opening trace file: No such file or directory (2)
04-07 15:32:41.414: E/AndroidRuntime(2001): FATAL EXCEPTION: main
04-07 15:32:41.414: E/AndroidRuntime(2001): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.quiz/com.example.quiz.Quizz}: java.lang.NullPointerException
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.os.Looper.loop(Looper.java:137)
04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-07 15:32:41.414: E/AndroidRuntime(2001): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 15:32:41.414: E/AndroidRuntime(2001): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 15:32:41.414: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-07 15:32:41.414: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-07 15:32:41.414: E/AndroidRuntime(2001): at dalvik.system.NativeStart.main(Native Method)
04-07 15:32:41.414: E/AndroidRuntime(2001): Caused by: java.lang.NullPointerException
04-07 15:32:41.414: E/AndroidRuntime(2001): at com.example.quiz.Quizz. <init>(Quizz.java:21)
04-07 15:32:41.414: E/AndroidRuntime(2001): at java.lang.Class.newInstanceImpl(Native Method)
04-07 15:32:41.414: E/AndroidRuntime(2001): at java.lang.Class.newInstance(Class.java:1319) 04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 04-07 15:32:41.414: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 04-07 15:32:41.414: E/AndroidRuntime(2001): ... 11 more
Your problem is actually this:
04-07 15:32:41.414: E/AndroidRuntime(2001): Caused by: java.lang.NullPointerException
04-07 15:32:41.414: E/AndroidRuntime(2001): at com.example.quiz.Quizz. <init>(Quizz.java:21)
I suspect that's somewhere in the beginning of your onCreate()
method - either when initialising the views or more likely cursor = adapter.getAllQuestionsParNiveau("Débutant");
. I don't see where the adapter is initialised ;)