无法启动 activity ComponentInfo,应用崩溃

Unable to start activity ComponentInfo, app crashes

当我 运行 使用此代码时,应用程序崩溃并且出现 "Unable to start activity ComponentInfo" 错误,我注意到在初始化游标时会发生这种情况。 这是我的 DBadapter returns 中的 Cursor 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);
}

这是我调用方法的activity:

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;
}

}

这是日志:

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.access0(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): 在 java.lang.Class.newInstance(Class.java:1319) 04-07 15:32:41.414: E/AndroidRuntime(2001): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1054) 04-07 15:32:41.414: E/AndroidRuntime(2001): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 04-07 15:32:41.414: E/AndroidRuntime(2001): ... 11 更多

你的问题其实是这样的:

 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)

我怀疑这是在您的 onCreate() 方法开头的某个地方 - 在初始化视图时或更有可能 cursor = adapter.getAllQuestionsParNiveau("Débutant");。我没有看到适配器在哪里初始化 ;)

您似乎没有初始化游标适配器。这就是导致空指针异常的原因。

您将 DBadapter 声明为适配器,但实际上并未对其进行初始化。因此,当您调用 adapter.whatever 时,您什么也得不到。